ARTICLE

Simple Factory Pattern using VB.Net

Posted by nimesh panchal Articles | Visual Basic 2010 January 10, 2003
A Simple Factory Pattern returns an instance of one of the several classes depending upon the parameters passed to the shared/non-shared factory method.
Download Files:
 
Reader Level:

A Simple Factory Pattern returns an instance of one of the several classes depending upon the parameters passed to the shared/non-shared factory method.

Typically, all these classes are inherited from a common parent class. Each of them has common methods. But each of them has their own optimization task operation on data.  

The following code implements the Simple Factory Pattern for the School Login System.

This application allows multi-access level user to login into the school system. Depending upon their userid/password they will be instantiated. 

SchoolPrincipal and SchoolTeacher are the classes derived from the SchoolUser base class. There is a UserFactory class which is responsible to instantiate the appropriate class instance (SchoolPrincipal or SchoolTeacher) depending upon the userid/password passed to its shared method GetSchoolUser. 

This method is entirely responsible for returning the appropriate class instance no matter how complex logic is involved. 

Diagram:

            

Base Class: 

Public Class SchoolUser
Public _Fname As String
Public _Lname As String
Public _UserType As String

Sub New()
End Sub
End
Class

Derived Classes:           

1. This class is for School Principal

Public Class SchoolPrincipal Inherits SchoolUser

Sub New()
_Fname = "David "
_Lname = "Smith "
_UserType = "Principal"
End Sub
End
Class

2. This class is for SchoolTeacher

Public Class SchoolTeacher Inherits SchoolUser

Sub New()
_Fname = "Patrecia"
_Lname = "Terry"
_UserType = "Teacher"
End Sub
End
Class

Factory Class: 

Public Class UserFactory

Shared Function GetSchoolUser(ByVal _login As String,_ByVal _paswrd As String) As SchoolUser
If _login = "principal" And _paswrd = "principal" Then
Return New SchoolPrincipal
ElseIf _login = "teacher" And _paswrd = "teacher" Then
Return New SchoolTeacher
Else
Throw New Exception
End If
End Function
End
Class

Client application using the factory: 

Private Sub Button1_Click(ByVal sender As System.Object,_ByVal e As System.EventArgs) Handles Button1.Click
Dim m_OUser As SchoolUser = UserFactory.GetSchoolUser(TextBox1.Text, TextBox2.Text)
MsgBox("User Type: " & m_OUser._UserType & vbNewLine & "Name: " & m_OUser._Fname & " " & m_OUser._Lname)
End Sub

Steps to run the project:

1. Unzip the SimpleFActory.zip

2. Build the solution.

3. Run the application. 

Enjoy!!

share this article :
post comment
 

This is a great example that's very easy to understand, however my question is: If I wanted to add a property called lineManager to the SchoolTutor class but not to the SchoolPrinciple class then how would I access this method in the client application. It seems to me that you can only use factory methods if the types being returned match the interface description and can not have additional properties. I realise that you can return a type with additional properties that derive from the Interface but the Client developing the application would not know what type of object is being returned in order to ctype it and gain access to the additional properties or functions added to the concrete class??

I'd really appreciate your input on this. 

-Gav

Posted by Gavin Kilbride Nov 17, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor