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
Join the conversation! Your thoughts help the community grow.