In object-oriented (OO) programs, we often see a mode called simple factory pattern ). The simple factory mode returns instances of one of several possible classes based on the data provided to it. Generally, the classes returned by the class have a common parent class and a common method, but each method is considered to be different and optimized based on different data. The simple factory mode does not actually belong to 23 gof modes, but it can be used as a guide to the factory method mode. How the factory model works To facilitate understanding the simple factory model, you can see:Creator: this role is the core of the factory method model and contains the creation of products according to certain business logic. The factory class creates a product object under direct calls from the client. It is often implemented by a specific class. Abstract Product role: the class that assumes this role is the parent class of the object created in the factory method mode, or the interfaces they share. Abstract product roles can be implemented using an interface or abstract class. Concrete Product role: Any object created in the factory method mode is an instance of this role. A specific product role is implemented by a specific class. In, Class X is a base class. Class XY and xz are derived from class X. The xfactory class decides to return the instance of that subclass based on the value you provide to it, we define the getclass method, accept some values, and return an instance of Class X based on this value. Returns which of them has nothing to do with programmers. Because they all contain the same method, but different implementations may be complicated functions, but they are usually very simple. Class Now we write two simple classes to split a string into two parts. Suppose we have an input form that allows users to enter their names, we can use the namefactory class in simple factory mode to automatically determine whether the firstname and lastname entered by the user are spaces or commas. If it is a comma or 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 are two Derived classes: Firstfirst and lastfirst They implement the base class nameclass and divide the name 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 Simple factory construction Now it is easy to give a simple factory class. Check whether a comma exists and return 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 Simple Factory The specific implementation is as follows: Click Event of 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 that allows users to enter names. Run Enter a name and click the "execute" button. The name is received by namefactory. Instances of different subclasses are returned based on different types (separated by commas and spaces. Here, we do not need to know which instance of the derived class is used. The factory (namefactory) provides us with this class. All we need to know is that it has two (getfirst, getlast) methods. This is what we call hiding a specific class. At the beginning, I learned how to use the design pattern in. net. I hope it will help you in the learning process! You can write to me for communication! |