The factory class is a class that is designed to create other objects, and factory classes are very important in the practice of polymorphism programming. It allows you to dynamically replace classes, modify configurations, and make your application more flexible. Mastering the factory model is essential for web development.
Factory mode is typically used to return different classes of similar interfaces, and a common use of factories is to create polymorphic providers.
Usually the factory pattern has a key construct, the static method that is generally named factory. This static method can accept any number of arguments and must return an object.
Program List: Basic Factory class
Program List: Using factory class production objects
Program List: A complete factory class
The following program defines a generic factory class that produces an empty object that can hold all of your operations, and you can get an instance of the operation that is in that instance.
class = $class; $this->constructor_args = Array_slice ($args, 1); } function __call ($method, $args) {$this->history[] = array (' Action ' =& Gt ' Call ', ' method ' = $method, ' args ' = $args); } function __set ($property, $value) {$this->history[] = array (' Action ' = ' Set ', ' property ' = $property, ' value ' = $value); }/** * Creates an instance and performs all operations that were do on this magicfactory */Function instance () {# Use Reflection to create a new instance, using the $args $REFL Ection_object = new Reflectionclass ($this->class); $object = $reflection _object->newinstanceargs ($this->constructor_args); # alternative method That doesn ' t is reflectionclass, but doesn ' t-support variable # Number of constructor parameters. $object = new $this->class (); # Repeat all remembered operations, apply to new object. foreach ($this->history as $item) {if ($item [' action '] = = ' call ') {Call_user_func _array (Array ($object, $item [' method ']), $item [' args ']; if ($item [' action '] = = ' Set ') {$object->{$item [' property ']} = $item [' value ']; }} # done return $object; }} class Fruit {private $name, $color; Public $price; function __construct ($name, $color) {$this->name = $name; $this->color = $color; } function SetName ($name) {$this->name = $name; } function introduce () { Print "Hello, this is an {$this->name} {$this->sirname}, it price is {$this->price} RMB."; }} # Setup a factory $fruit _factory = new Fruitfactory (' Fruit ', ' Apple ', ' gonn '); $fruit _factory->setname (' Apple '); $fruit _factory->price = 2; # Get An instance $apple = $fruit _factory->instance (); $apple->introduce ();? >
Program Run Result:
Hello, this was an Apple, it price is 2 RMB.
The factory model provides a transition interface for creating objects to isolate the concrete process masks for creating objects to achieve greater flexibility.
The factory model can be divided into three categories:
- Simple Factory mode (Factory)
- Factory mode (Factory method)
- Abstract Factory mode (Factory)
These three modes are progressively abstracted from top to bottom and more general.
The simple factory model is also called the Static factory method mode. Renaming can tell that this pattern must be simple. The purpose of its existence is simple: Define an interface for creating objects. The factory method pattern removes the static property of the factory method in the simple Factory mode, allowing it to inherit from the quilt class. The pressure to concentrate on factory methods in a simple factory model can be shared by different factory subclasses in the factory method model.
The factory method pattern seems to have been perfectly wrapped in the creation of objects, allowing the client to process only the interfaces provided by the abstract product role. Do we have to be in the code all over the factory? It doesn't have to be big. Perhaps you might consider using the factory method mode under the following conditions:
- When the client program does not need to know the creation process to use the object.
- The object used by the client is subject to change, or there is no knowing which specific object to use.
http://www.bkjia.com/PHPjc/752390.html www.bkjia.com true http://www.bkjia.com/PHPjc/752390.html techarticle The factory class is a class that is designed to create other objects, and factory classes are very important in the practice of polymorphism programming. It allows you to dynamically replace classes, modify configurations, and make applications more ...