Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  Windows Development  »  Simple Factory Pattern in VB.NET
Simple Factory Pattern in VB.NET
by Manish Mehta | Published  11/15/2001 | Windows Development | Rating:
Manish Mehta

Manish Mehta has 3 years experience Microsoft Technologies with VB6, ASP, COM+. Presently engaged with Syntel India Ltd as a Programmer Analyst deputed in the elite Advanced Technology Group (ATG). Manish is a Microsoft Certified Professional.

He is also the founder of www.dotnetextreme.com a site dedicated to disseminate information regarding .NET and related technologies.

 

View all articles by Manish Mehta...
Simple Factory Pattern in VB.NET

In this series of upcoming articles I will be discussing how we can implement most of the common and widely used design patterns in .NET.

Patters are broadly classified into 3 categories:

  • Creational Patterns
  • Structural Patterns
  • Behavorial Patterns

This first article in the series explains how we can implement a "Simple Factory" pattern, which falls in of type Creational Patterns.

The Simple Factory Pattern

A Simple Factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it. Usually all of the classes it returns have a common parent class and common methods, but each of them performs a task differently and is optimized for different kinds of data. It is fairly easy to implement a simple factory method using VB.NET

We will build a Simple Factory which accepts the Name of a person and splits it into First name and Last name depending on the format of the name passed for e.g. Bill Gates or Gates,Bill i.e FirstName LastName or LastName,FirstName

The logic to split the full name would be built in two classes FirstFirst and LastLast whose instance would be created by the simplefactory class depending on the name format passed. We will create a base class, which will have two functions getFirst and getLast to get the FirstName and LastName respectively.

Imports System
Imports Microsoft.VisualBasic

    Public Class NameClass
    Protected LnameFrname As String

    Public Function getFirst() As String
        Return Frname
    End Function
    
    Public Function getLast() As String
        Return Lname
    End Function

End Class
Generated using PrettyCode.Encoder

Then, we create a FirstFirst class which has a logic to split the name if it is in the format "Bill Gates", this class derives from the Name class.

Public Class FirstFirst

    Inherits NameClass

    Public Sub New(ByVal nm As String)
    Dim arr() As String

        arr = Split(nm, " ")
        Frname = arr(0)
        Lname = arr(1)

    End Sub

End Class
Generated using PrettyCode.Encoder

The next step is to create the LastLast class which has a logic to split the name if it is in the format "Gates,Bill", this class also derives from the Name class.

Public Class LastLast

    Inherits NameClass

    Public Sub New(ByVal nm As String)
    Dim arr() As String

        arr = Split(nm, ",")
        Frname = arr(1)
        Lname = arr(0)

    End Sub

End Class
Generated using PrettyCode.Encoder

Our final step is to create the Simple Factory class which will accept the Name and decide instance of which of the two derived classes will be created.

Public Class SimpleFactory
    Shared Sub main()
    Dim strName As String
    Dim o As NameClass
        Console.WriteLine("Name")
        strName = Console.ReadLine
        If strName Like ("*,*"Then
            o = New LastLast(strName)
            MsgBox(o.getFirst)
            MsgBox(o.getLast)
        Else
            o = New FirstFirst(strName
            MsgBox(o.getFirst
            MsgBox(o.getLast)
        End If
    End Sub
End Class
Generated using PrettyCode.Encoder

Copy all the codes in a single file SimpleFactory.vb and compile as

vbc SimpleFactory.vb

Execute the SimpleFactory.exe and supply the name in the formats Bill Gates and Gates,Bill
What you will notice is the factory class determines the input and decides which object to create.

Related links: http://www.dotnetextreme.com

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:2.88235294117647 out of 5
 51 people have rated this page
Article Score23137
Comments    Submit Comment

Comment #1  (Posted by Jonathan Baggaley on 01/19/2004)

Do you know of any samples of how to implement a factory process when using multiple assemblies/dlls in vbNet2003? I want to create a variety of calculation components (engines) which will be shipped out e.g. calcengine1, calcengine2, calcengine3. Each of these will have exactly the same interface. The controller for these looks at what engines it has available from a list held in a configuration file, and then opens and calls the relevant DLL for calculation. e.g. the first time it runs through, engines 1 and 2 are available but the next time, only 1 and 3 are available.
All help would be greatfully appreciated.

Thanks

Jon
 
Comment #2  (Posted by an unknown user on 02/11/2005)
Rating
very good example to explain factory class?
 
Comment #3  (Posted by an unknown user on 03/21/2005)
Rating
I would like to know more on this factory class I liked the clear explaination
Anyway Thanks
 
Comment #4  (Posted by an unknown user on 04/08/2005)
Rating
move the message boxes out of the if then construction


 
Comment #5  (Posted by an unknown user on 04/25/2005)
Rating
You need to talk more about real world usage. I understand that it is good to have a succinct example, but I think this example does not show the power of the factory pattern.

 
Comment #6  (Posted by an unknown user on 05/25/2005)
Rating
Very Explanatory and easy to understand
 
Comment #7  (Posted by an unknown user on 06/19/2005)
Rating
Manish, You got to keep reference to original document or book that are you getting / copying materials from.

By reading this article there is a feeling that you are writting this topic of your own while you have copied every single line of the code from the book Cooper, James William, 1943- C# design patterns : a tutorial / James W. Cooper.

It is a really good book and as cortesry please you are supposed to reference to this valuable book.
 
Comment #8  (Posted by an unknown user on 03/06/2006)
Rating
Graet Article
 
Comment #9  (Posted by an unknown user on 05/12/2006)
Rating
Not very useful to get your object instances in a message box.
Edward Grant.
 
Comment #10  (Posted by an unknown user on 07/25/2006)
Rating
Not very useful info..Kundana lal
 
Comment #11  (Posted by an unknown user on 12/14/2006)
Rating
Just what I needed
 
Comment #12  (Posted by an unknown user on 03/07/2007)
Rating
I think this example sux. Any new person who is trying to learn would surely get confused.
 
Comment #13  (Posted by an unknown user on 03/08/2007)
Rating
useless
 
Comment #14  (Posted by an unknown user on 04/18/2007)
Rating
We need this in c# as well
 
Comment #15  (Posted by an unknown user on 05/23/2007)
Rating
Good example and explaination
 
Comment #16  (Posted by an unknown user on 06/10/2007)
Rating
good for innitial learner
 
Comment #17  (Posted by sudhir on 06/10/2007)
Rating
good for initial learner, can you send me better example on my e-mail
 
Comment #18  (Posted by an unknown user on 09/23/2007)
Rating
wanted to learn how to split a string with title,fullname,middlename,lastname.was able to do it successfully using th eabove code
 
Comment #19  (Posted by an unknown user on 02/13/2008)
Rating
Instead of dealing with two different string parameter formats would it not have made more sense to deal with something a bit more straight forward?

While the general idea is obtained from this example, it could be something that was much more tangible. Secondly, this article seems to infer that you need an abstract base class - is that the case always?


 
Comment #20  (Posted by an unknown user on 05/22/2008)
Rating
He May give a better example than this
 
Comment #21  (Posted by an unknown user on 11/12/2008)
Rating
re41bsv3fkydz9hb
 
Comment #22  (Posted by an unknown user on 11/13/2008)
Rating
re41bsv3fkydz9hb
 
Comment #23  (Posted by Eduardo on 03/25/2009)
Rating
I think it's really poor to present a factory based on a select statement.
 
Comment #24  (Posted by an unknown user on 11/22/2009)
Rating
not so informational 4 the begginers.
 
Comment #25  (Posted by an unknown user on 03/22/2010)
Rating
good article to explain factory method. kuber
 
Comment #26  (Posted by buy cheap oem software on 02/12/2012)
Rating
6PxFO0 Develop the topic further! It is interesting to know more details..!!
 
Sponsored Links