Simple factory pattern (Simple Factory Pattern)

zhaozj2021-02-16  55

Factory model

The factory pattern

In an object-oriented (OO) program, what we often see is Simple Factory Pattern. Simple factory mode returns an instance of a class in a few possible classes based on the data provided to it. Usually it returns a class having a common parent class and a common method, but each method is considered different, and it is optimized according to different data. The simple factory model is actually not 23 GOF mode, but it can be guided by a Factory Method.

How does the factory model work?

In order to understand the simple factory model, you can see the figure below:

Factory (CREATOR) Role: This role is the core of factory method model, which contains products in accordance with certain business logic. The factory class creates a product object under the direct call of the client, which is often implemented by a specific class.

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

Concrete Product Role: Any object created by the factory method mode is an instance of this role, and the specific product role is implemented by a specific class.

In the above figure, class X is a base class, class XY, and XZ derived self-class X, XFactory class decides to return the instance of the subclass based on the value you provide, we define the getClass method, accept some value, and Returns one of the X classes based on this value. Returns which one of them is not related to programmers. Because they all contain the same method, different implementations may be a very complex function, but it is usually very simple.

class

Now we write two simple classes, which are used to implement a string into two parts. Suppose we have an input form, allowing users to enter his name, but we can judge user input through simple factory model NameFactory class The firstname and lastname are spaced or comma. If we are a comma or space, we divide them 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 is written two derived classes: FirstFirst Class, LastFirst classes They implement base class NameClass, and divide the names into two parts in the constructor.

Firstfirstfirst

Public Class FirstfirstFirst

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 integeri = 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

Texture simple factory

It is easy to give a simple factory class now. Only the comma is existing, then returns an instance of one of them,

Public Class NameFactory

Public function getname (_

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

We are implemented as follows:

"Execute" button Click event:

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 allow the user to enter the name. Run as shown:

The user enters the name and click the "Execute" button, which is received by NameFactory and returns an instance of different subclasses based on different types (separated by commas, spaces). It doesn't matter which derived class is used here, the factory (NameFactory) provides us with this class, what you need is to have two (getFirst, getlast) methods. This is what we have said to hide the specific class.

I just started learning the design mode in .NET, I hope to help everyone in the learning process!

Can write let me exchange!

转载请注明原文地址:https://www.9cbs.com/read-23475.html

New Post(0)