PHP Factory modeConcept: Factory mode is a class that has some methods for creating objects for you. You can use the factory class to create objects without using new directly. This way, if you want to change the type of object you are creating, you can simply change the factory. All code that uses the factory is automatically changed.
Depending on the level of abstraction, the PHP factory model is divided into: Simple Factory mode, factory method mode, and abstract Factory mode
Simple Factory mode:
/** * Simple Factory mode comparison with factory method mode. * Simple Factory is also called Static factory method mode, so the understanding can be determined that the simple factory pattern is created by a static method of the object. */interface People { function Jiehun ();} Class Man implements people{ function Jiehun () { echo ' send roses, send ring! <br> '; }} Class women implements people { function Jiehun () { echo ' wears a wedding dress! <br> '; }} static method in class Simplefactoty {//simple factory statically function Createman () { return new man ; } static function Createwomen () { return new women; } } $man = Simplefactoty::createman (); $man Jiehun (); $man = Simplefactoty::createwomen (); $man->jiehun ();
Factory method Mode:
<?php/* * Factory Method Mode: * Defines an interface for creating objects, letting subclasses decide which class to instantiate. He can solve the problem of closed open principle in simple Factory mode. <www.phpddt.com Finishing > */interface people { function Jiehun ();} Class Man implements people{ function Jiehun () { echo ' send roses, send ring! <br> '; }} Class women implements people { function Jiehun () { echo ' wears a wedding dress! <br> '; }} Interface Createman { //note, here is the essential difference of the simple factory, which abstracts the creation of objects into an interface. function Create ();} Class Factoryman implements createman{ function Create () { return new Man; }} Class Factorywomen implements Createman { function Create () { return new Women; }} class Client { static method in c20/>//Simple factory function test () { $Factory = new Factoryman; $man = $Factory->create (); $man->jiehun (); $Factory = new factorywomen; $man = $Factory->create (); $man->jiehun (); }} $f = new Client; $f->test ();
Abstract Factory mode:
<?php/* Abstract Factory: Provides an interface to create a series of related or interdependent objects. Note: The difference between this and the factory method is: A series, and the factory method is one. So, can we think of adding a way to create a "series" of objects in interface create? */interface people {function Jiehun ();} Class Oman implements people{function Jiehun () {echo ' Belle, I send you roses and rings! <br> '; }}class Iman implements people{function Jiehun () {echo ' I secretly like you <br> '; }} class Owomen implements people {function Jiehun () {echo ' I'm going to wear a wedding dress! <br> '; }} class Iwomen implements people {function Jiehun () {echo ' I'm so shy Oh!! <br> '; }} interface Createman {//note, here is the essential difference, which abstracts the creation of an object into an interface. function Createopen (); It is divided into introverted and extroverted function Createintro (); Inward}class Factoryman implements createman{function Createopen () {return new Oman; } function Createintro () {return new Iman; }}class Factorywomen implements Createman {function Createopen () {return new Owomen; } function Createintro () {return new Iwomen; }} class Client {//Simple factoryThe static method function test () {$Factory = new Factoryman; $man = $Factory->createopen (); $man->jiehun (); $man = $Factory->createintro (); $man->jiehun (); $Factory = new Factorywomen; $man = $Factory->createopen (); $man->jiehun (); $man = $Factory->createintro (); $man->jiehun (); }} $f = new Client; $f->test ();
Difference:
Simple Factory mode: used to produce any product in the same hierarchy. The inability to add new products to the
Factory mode: Used to produce fixed products in the same grade structure. (Support for adding any product)
Abstract Factory: Used to produce all products of different product families. (for adding new products, powerless; Support for adding product families)
The above three plant methods have different levels of support in two directions: hierarchical structure and product family. So consider which method to use depending on the situation.
Scope of application:
Simple Factory mode:
The factory class is responsible for creating fewer objects, and the customer knows only the parameters of the incoming factory class and does not care about how to create the object.
Factory method Mode:
A factory method pattern can be used when a class does not know that it must create an object's class or a class that expects subclasses to specify the object it creates, when the class delegates the responsibility of creating an object to one of several helper subclasses, and you want to localize the information to which helper subclasses are proxies.
Abstract Factory mode:
A system should not depend on the details of the product class instance being created, composed and expressed, which is important for all forms of the factory model. This system has more than one product family, and the system only consumes one of the product families. Products belonging to the same product family are used together, and this constraint must be reflected in the design of the system. The system provides a library of product classes, with all products appearing on the same interface, so that clients do not rely on implementations.
Whether it is a simple factory model, a factory model, or an abstract factory model, they essentially extract the invariant parts and leave the variable portions as interfaces for maximum reuse. Which design pattern is more appropriate, depends on the specific business needs.