Learn PHP Design patterns PHP Implementation Factory mode (factory) _php tips

Source: Internet
Author: User
Tags learn php php example

I. Intention
Defines an interface for creating objects, letting subclasses decide which class to instantiate. Factory method uses an instantiation of a class to defer to its subclass "GOF95"
Second, the factory pattern structure chart

Three, main roles in Factory mode
Abstract Product Role: parent class or interface common to specific product objects
Specific product (concrete product) Role: implements interfaces defined by the abstract product role, and each object created by the factory method pattern is an instance of a specific product object
Abstract Factory (Creator) Role: This interface is implemented by any factory class that creates objects in the schema, and it declares the factory method, which returns an object of type product.
Creator can also define a default implementation of a factory method that returns a default Concreteproduct object
Specific factory (concrete Creator) role: Implements the abstract factory interface, the specific factory role is related to the application logic, Called directly by the application to create a Product object.
Four, advantages and disadvantages of the factory model
The Factory mode benefits:
The factory method mode allows the system to introduce new products without modifying the factory role.
Factory mode disadvantage:
Customers may have to create a creator subclass
Five, just to create a specific Concreteproduct object. The Factory mode applies to the scenario
1, when a class does not know the class of the object it must create
2, when a class expects its subclass to specify the object it creates
3, when the class delegates the responsibility for creating an object to one of several helper subclasses , and the information that you want to be a surrogate is a proxy.
Six, Factory mode and other schemas
Abstract Factory mode (abstract Factory mode) : Abstract Factory mode often uses factory methods to implement
Template Method mode: Factory methods are usually called in Template methods
Seven, Factory mode PHP Example

<?php/** * Abstract Factory role * * Interface Creator {public function FactoryMethod ();} /** * Specific Factory role A */class Concretecreatora implements Creator {/** * Factory method return specific product A * @return Concreteproducta */PU
 Blic function FactoryMethod () {return new concreteproducta ();
 }/** * Specific factory role B/class Concretecreatorb implements Creator {/** * Factory method return specific product B * @return CONCRETEPRODUCTB
 */Public Function FactoryMethod () {return new CONCRETEPRODUCTB ();           
}/** * Abstract product Role */interface Product {Public function operation (); /** * Specific product role A/class Concreteproducta implements product {/** * interface method implementation output specific string/public function operation
 () {echo ' concreteproducta <br/> '; }/** * Specific product role B/class CONCRETEPRODUCTB implements product {/** * interface method implementation output specific String */Public function Operati
 On () {echo ' concreteproductb <br/> ';
 } class Client {/** * Main program. */public static function main () {$creatorA = new ConcretecreatoRA ();
 $productA = $creatorA->factorymethod ();
 
 $productA->operation ();
 $creatorB = new Concretecreatorb ();
 $productB = $creatorB->factorymethod ();
 $productB->operation (); } client::main ();?>

Viii. distinguishing factory method patterns from simple factory models
The difference between the factory method model and the simple factory model structure is not obvious. The core of the Factory method class is an abstract factory class, while the simple factory model puts the core on a specific class.
The factory method pattern has an alias called the Polymorphism factory pattern because the specific factory class has a common interface, or there is a common abstract parent class.
When the system extension needs to add a new product object, only need to add a specific object and a specific factory object, the original factory objects do not need to make any changes, and do not need to modify the client, well in line with the "open-closed" principle. The simple factory model has to modify the factory method after adding the new product object, and the extensibility is not good.
The factory method model can evolve into a simple factory model after degradation.

The above is the use of PHP to implement the factory model code, there are some of the concept of the factory model to distinguish, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.