Article Options
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 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
        strName = Console.ReadLine
        If strName Like ("*,*"Then
            o = New LastLast(strName)
            o = New FirstFirst(strName
        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:

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.95238095238095 out of 5
 63 people have rated this page
Article Score29895
Sponsored Links