Simple Factory Pattern
来源:互联网 发布:大型数据库课程设计 编辑:程序博客网 时间:2024/06/04 18:02
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
Enjoy
- Simple Factory Pattern
- Simple Factory Pattern
- Simple factory pattern
- Design Pattern :Simple Factory
- Design Pattern: Simple Factory 模式
- 简单工厂 Simple Factory Pattern
- Simple Factory(Static Factory) Design Pattern
- Creational Pattern之simple factory pattern
- 简单工厂模式(Simple Factory Pattern)
- 简单工厂模式 Simple Factory Pattern
- 简单工厂模式(simple Factory Pattern)
- 简单工厂模式(simple factory pattern)
- 一、简单工厂模式(Simple Factory Pattern)
- 简单工厂模式(Simple Factory Pattern)
- 简单工厂模式(Simple Factory Pattern)
- 1.简单工厂模式 Simple Factory Pattern
- 简单工厂模式 Simple Factory Pattern
- 简单工厂模式(Simple Factory Pattern)
- 高分求HttpWebRequest使用axWebBrowser的session 和cookier的解决办法
- 只想给自己确立个目标!
- Single Disign Pattern
- 使用Template的对Cpp程序的影响
- vs.net常用快捷键
- Simple Factory Pattern
- 关于大学学生干部的一点感想
- VSS使用实例1
- C#名字空间
- 软件构架的模型
- 复合(composite)和聚集(Aggregation)
- DataGrid自定义分页存储过程
- PhotoShop滤镜
- 涂鸦,又见涂鸦...