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 Lname, Frname As String
Public Function getFirst() As String
Return Frname
End Function
Public Function getLast() As String
Return Lname
End Function
End Class
|
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
|
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
|
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
|
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