The simple Factory mode is also called the Static factory method pattern, and the main function is to instantiate (create) objects of each class through a simple factory class, without having to instantiate the object through new. The advantage is that the factory class contains certain logical judgments that dynamically instantiate related classes based on the client's selection criteria. The downside is that when you need to add a new feature class, you need to modify the factory class.
The following is a case study of a simple calculator program. The first step, defining operation, is a parent class with two properties representing the two parameters used for the calculation.
Numa= $numA;} Public Function Getnuma () {return $this->numa;} Public Function Setnumb ($numB) {$this->numb= $numB;} Public Function Getnumb () {return $this->numb;}}? >
The second step is to define an interface that declares the method of implementing the Operation
The third step is an addition operation class (Omit subtraction class, multiplication class, trigger class, etc.)
Getnuma () + $this->getnumb (); return $result;}}? >
Finally, you define a simple factory class that is used to create object instances of various classes. Normally the objects returned by the simple factory class have a common parent class. In this example, the public parent class is the Operation class, and the addition and subtraction classes are subclasses of the Operation class.
The client code is as follows:
Setnuma (2); $op->setnumb (4); Echo $op->getresult (); $om =simplefactory::createminus (); $om->setnuma (45); $ Om->setnumb, echo "
"; Echo $om->getresult ();? >
The above introduces the simple factory model of PHP implementation, including the simple Factory mode content, I hope the PHP tutorial interested in a friend helpful.