Simple factory pattern (Simple Factory Pattern)

xiaoxiao2021-03-06  88

The simple factory mode is based on the data provided to it, returns an instance of a class in a few possible classes. Usually it has a common parent class and a common method, but the task performed by each method is different, and Optimized according to different data. For example, X is a base class, XY and XZ are derived from it, the XFactory class decides to return the subclass according to the parameters given, and returns the instance of that class and the programmer, because these classes have the same method, It is only different, and the instance of returning that class depends on the factory, factory functions may be complicated, but it is usually quite simple.

Next, we use an example to explain this simple factory model. If we want to enter a person's name,

There are two ways, one is "firstname lastname" and "FRISTNAME, Lastname" form. Our task is to determine if LastName and Firstname are comma to determine the name.

The first step is to design a parent class:

Public class cname {

Protected stringfname, lname

Public string getFristname () {returnfrname;}

Public string getlastname () {return lname;}

}

In the second step, design two subclasses, which is derived from class CNAME. They realized the interface and divided the name into two parts in the construction function. In the cfirstfirst type, a simple assumption is made: all parts of the last space are in front of firstname.

Public class cfirstfirst: cname {

Public cfirstfirst (String sname) {

INT i = sname.trim (). Indexof ("");

IF (i> 0) {

FRNAME = Name.substring (0, i) .trim ();

Lname = name.substring (i 1) .trim ();

}

Else {

Lname = sname;

FRNAME = ""

}

}

}

In the Lastfist class, use a calm number to divide the limit. When the space or comma does not exist, both classes provide error correction processing.

Public class lastfrist: cname {

Public lastfrist (string sname) {

INT i = sname.indexof (",");

IF (i> 0) {

IF (i> 0) {

Lname = name.substring (0, i) .trim ();

FRNAME = Name.substring (i 1) .trim ();

}

Else {

Lname = sname;

FRNAME = ""

}

}

}

In both cases, the separate name is stored in the CNAME of the base class. So there is no need for any getFirstName and getLastName methods at all in the derived class because the base class has been given.

Step 3: Construct a simple factory.

It is now easy to give a simple factory class. Only check the comma to decide to return the instance of that class

Public class namefactory ()

{

Public cname getname (String sname) {

INT i = sname.indexof (",");

IF (i> 0)

Return New Lastfrist (Sname);

Else

Return New CfirstFirst (Sname);

}

}

the fourth step. Use factory class:

String sname = "cedy hao";

Namefactory nf = new namefactory (); cname name = nf.getname (sname);

String sfristname = name.getFristname ();

String Slastname = name.getlastname ();

This method is separated from the data related issues with other methods of the class.

One next in the experiment "factory method mode (Factory Method Pattern".

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

New Post(0)