Statement: This series of blog reference "Big Talk design mode", author Geoscience.
The first two sections describe what is the design pattern and six principles, I believe that the previous two sections of the content of the design pattern has been a preliminary understanding, the next to say a design pattern classification.
In general, the object-oriented design pattern is divided into three categories: creation, structure, behavior three kinds.
creation: When objects are created, they are no longer instantiated directly by us, but are determined by the program to determine how objects are created based on a particular scenario, thus guaranteeing greater performance and better architectural advantage. The creation pattern has a simple Factory mode (not one of 23 design patterns), a factory method, an abstract Factory mode, a singleton mode, a generator mode, and a prototype mode.
structured: Used to help organize multiple objects into larger structures. The structure mode includes adapter mode, bridge mode, combination mode, adorner mode, façade mode, Hengyuan mode and proxy mode.
behavioral: Used to help communicate the objects between systems, and how to control processes in complex systems. Behavioral patterns include command mode, interpreter mode, iterator mode, mediator mode, Memo mode, observer mode, State mode, policy mode, template mode, visitor mode, and responsibility chain mode.
Today we mainly introduce the first simple factory model of the creation type.
Note: Be sure to read UML class diagrams, Object-oriented PHP programming basics When reading this series of blogs.
Simple Factory mode is not one of the most commonly used object-oriented design patterns. A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns. The essence is that a factory class dynamically determines which product class (these product classes inherit from a parent class or interface) should be created , based on the parameters passed in .
Roles and Responsibilities:
Factory ( simplefactory role: The core of the simple factory pattern, which is responsible for implementing the internal logic of creating all instances. The factory class can be called directly by the outside world to create the desired product object.
Abstract Products ( iproduct role: The parent class of all objects created by the simple Factory mode, which is responsible for describing common interfaces common to all instances.
Specific Products ( Concrete Product role: Is the creation target of the simple factory pattern, and all created objects are instances of a specific class that acts as the role.
Requirement: Create product objects with corresponding attributes from a simple factory based on providing the corresponding attribute values.
Now according to the above UML class diagram to write the following PHP code.
<?php/** * Created by Phpstorm. * User:jiang * DATE:2015/4/9 * time:21:48 *//** Abstract Product Role * Interface IProduct Product interface */interface iproduct{/**x axis rotation * @return Mixed * * function xrotate (); /**y Axis Rotation * @return Mixed */function yrotate ();} /** Specific Product Role * Class xproduct x Axis rotation product */class Xproduct implements iproduct{private $xMax = 1; Private $yMax = 1; function __construct ($xMax, $yMax) {$this->xmax= $xMax; $this->ymax=1; } function Xrotate () {echo "Hello, I am x axis rotation product, X axis to turn ... "; } function Yrotate () {echo "Sorry, I am the x axis rotation product, I do not have the Y axis ... "; }}/** Specific Product Role * Class yproduct y-axis rotation product */class Yproduct implements iproduct{private $xMax = 1; Private $yMax = 1; function __construct ($xMax, $yMax) {$this->xmax=1; $this->ymax= $yMax; } function Xrotate () {echo "Sorry, I'm a y-axis spin product, I don't have an x-axis ... "; } function Yrotate () {echo "Hello, I am y axis rotation product, Y axis to turn ... "; }}/** Specific Product Roles *Class xyproduct XY axis can be rotated products */class Xyproduct implements iproduct{private $xMax = 1; Private $yMax = 1; function __construct ($xMax, $yMax) {$this->xmax= $xMax; $this->ymax= $yMax; } function Xrotate () {echo "Hello, I am xy axis are rotatable products, x-axis to turn ... "; } function Yrotate () {echo "Hello, I am xy axis are rotatable products, y-axis to turn ... "; }}/** Factory Role * Class productfactory */class productfactory{static function getinstance ($xMax, $yMax) {if ($xMax & Gt;1 && $yMax ===1) {return new xproduct ($xMax, $yMax); } elseif ($xMax ===1 && $yMax >1) {return new yproduct ($xMax, $yMax); } elseif ($xMax >1 && $yMax >1) {return new xyproduct ($xMax, $yMax); } else {return null; } }}
Test Code:
<?php/** * Created by Phpstorm. * User:jiang * DATE:2015/4/9 * time:21:54 */require_once "./simplefactory/simplefactory.php"; Header ("Content-Type: Text/html;charset=utf-8 "), $pro =array (), $pro []=productfactory::getinstance (1,12); $pro []=productfactory:: GetInstance (12,1); $pro []=productfactory::getinstance (12,12); $pro []=productfactory::getinstance (0,12); foreach ($ Pro as $v) { if ($v) { echo "<br/>"; $v->xrotate (); echo "<br/>"; $v->yrotate (); } else { echo "illegal product! <br/> "; } echo "
Using the browser to access the test code, we can find that the object created is Yproduct,xproduct,xyproduct,null. The core code of a simple factory is the role of the Factory (Productfactory), which creates different objects based on the incoming xmax and ymax values, which is the essence of the simple factory, and we don't know what the specific product class is at all in the test call client. This makes it possible to detach the call from the creation.
The advantage of a simple factory is that it separates the object's caller from the object creation process, and requests it directly to the factory when the object caller needs the object. in order to improve the maintainability and expansibility of the system, the object's caller and the object's implementation class are coupled in hard coding mode.
Disadvantages of a simple factory: when the product changes, the factory class also to do the corresponding changes, such as to add an operation class, such as the number of M-N-Square, you have to change case, modify the original class, violates the open-closed principle .
PHP design mode-Simple factory