Simple factory model of design pattern (plain Factory pattern)

Source: Internet
Author: User
Tags abstract integer trim
Design Factory mode

The FACTORY pattern



One of the patterns that we often see in object-oriented (OO) programs is the Simple factory model (simplicity Factory pattern). The Simple factory pattern returns an instance of a class in several possible classes, based on the data provided to it. Typically, the classes it returns have a common parent class and a common method, but each method performs differently and is optimized according to different data. The simple factory pattern does not actually belong to 23 gof modes, but it can be a boot to the factory method pattern (FACTORY methods).



How Factory mode Works



To facilitate understanding of the simple factory model, you can look at the following figure:



Factory Class (Creator) Role: The role is the core of the factory approach model, containing products created according to certain business logic. The factory class creates product objects under a direct call from the client, which is often implemented by a specific class.

Abstract Product Role: The class that holds this role is the parent class of objects created by the factory method pattern, or the interfaces they share. Abstract product roles can be implemented with an interface or abstract class.

Specific product (concrete product) Role: Any object created by the factory method pattern is an instance of this role, and the specific product role is implemented by a specific class.




In the diagram above, Class X is a base class, class XY and XZ are derived from Class X, and the Xfactory class decides to return the instance of that subclass based on the value you give it, we define the GetClass method, accept some values, and return one instance of the X class based on that value. Return which one of them is not related to the programmer. Because they all contain the same method, but different implementations, it may be a complex function, but it is usually simple.

Class

Now, let's write two simple classes that implement dividing a string into two parts, assuming we have an input form that allows the user to enter his name, We can automatically judge the FirstName and LastName between the user input and the comma by the simple factory pattern Namefactory class. If it is a comma or a space, we divide it into two parts.
First, define a simple class.

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

The following two derived classes are written:
Firstfirst class, LastFirst class
They implement the base class Nameclass and divide the names into two parts in the constructor.



Firstfirst class



Public Class Firstfirst

Inherits Nameclass

Public Sub New (ByVal nm as String)

Dim I as Integer

i = nm. IndexOf ("")

If i > 0 Then

Frname = nm. Substring (0, I). Trim ()

Lname = nm. Substring (i + 1). Trim ()

Else

Frname = ""

LName = NM

End If

End Sub

End Class



LastFirst class



Public Class LastFirst

Inherits Nameclass

Public Sub New (ByVal nm as String)

Dim I as Integer

i = nm. IndexOf (",")

If i > 0 Then

Lname = nm. Substring (0, I). Trim ()

Frname = nm. Substring (i + 1). Trim ()

Else

Frname = ""

LName = NM

End If

End Sub

End Class



Construct a simple factory



It is now easy to give a simple factory class. Detects only whether a comma exists, and then returns an instance of one of the classes.



Public Class Namefactory

Public Function Getnamer (_

ByVal nm as String) as Nameclass

Dim I as Integer

i = nm. IndexOf (",")

If i > 0 Then

return New LastFirst (nm)

Else

return New Firstfirst (nm)

End If

End Function

End Class



Use Simple Factory



Our specific implementation is as follows:






Click event for the Execute button:



Dim nm as String
Dim I as Integer
NM = Txname.text
Dim Nmer as Nameclass
Nmer = New namefactory (). Getnamer (NM)

Txlast.text = Nmer.getlast ()
Txfirst.text = Nmer.getfirst ()



We have constructed a simple user interface and allowed the user to enter a name. Run as shown:






The user enters the name and clicks the "Execute" button, which is received by Namefactory, and returns instances of the different subclasses according to different types (separated by commas and spaces). There is no need to know which instance of the derived class is being used, and the Factory (Namefactory) provides us with this class, and all you need to know is that it has two (GetFirst, GetLast) methods. This is what we call hiding the specific class.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.