PHP design mode (iii) abstract Factory mode (abstracted Factory for PHP)

Source: Internet
Author: User

First, what is abstract Factory mode

Abstract Factory mode is intended to provide an interface to the client to create product objects in multiple product families, and the use of abstract Factory mode also satisfies the following conditions:

    1. There are multiple product families in the system, and the system can only consume one family of products at a time.
    2. Products that belong to the same product family can be used.

  Product families: A family of products that are associated with functions in different product grade structures. The following example of car and air conditioning is two product tree, Mercedes-Benz C200+ Gree a model of air conditioning is a product family, similarly, the Audi a4+ Haier model air conditioning is also a product family.

Second, class diagram

Three, the composition of the abstract factory model

    1. Abstract Factory (abstractfactory): Determine the business scope of the plant.
    2. Specific Factory (concretefactory): Each specific plant corresponds to a product family. The specific factory determines which specific product object to produce.
    3. Abstract product (ABSTRACTPRODUCT): An abstract class of the same product hierarchy.
    4. Specific Products (CONCRETEPRODUCT): Specific products available for production.

Product Category:

<?PHP//Automotive (abstract product interface)Interfaceautoproduct{ Public functiondirve ();}//Audi A4 (specific product category)classAudia4productImplementsautoproduct{//Get car name     Public functionDirve () {Echo"Open the Audi A4." <br> "; }}//Mercedes-Benz C200 (Specific product category)classBenzc200productImplementsautoproduct{//Get car name     Public functionDirve () {Echo"Drive the Mercedes-Benz C200". <br> "; }}?>

<?PHP//Air Conditioning (abstract product interface)Interfaceaircondition{ Public functionblow ();}//gree Air Conditioning a model (specific product category)classGreeairconditionImplementsaircondition{ Public functionBlow () {Echo"Blowing gree air conditioning a model". <br> "; }}//Haier Air-conditioning a model (specific product category)classHaierairconditionImplementsaircondition{ Public functionBlow () {Echo"Blowing Haier air conditioning a model". <br> "; }}?>

Factory class:

<?PHP//Factory InterfaceInterfacefactory{ Public functionGetauto ();  Public functiongetaircondition ();}//Factory A = Audi A4 + Haier Air conditioning a modelclassAfactoryImplementsfactory{//Automotive     Public functionGetauto () {return Newaudia4product (); }    //Air Conditioning     Public functiongetaircondition () {return Newhaieraircondition (); }}?>
<?PHP//Factory B = Mercedes-Benz C200 + gree Air conditioning a modelclassBfactoryImplementsfactory{//Automotive     Public functionGetauto () {return Newbenzc200product (); }    //Air Conditioning     Public functiongetaircondition () {return Newgreeaircondition (); }}?>

Client testing:

<?PHP//Client Test Code$factoryA=Newafactory ();$factoryB=Newbfactory ();//a factory-made car$auto _cara=$factoryA-Getauto ();$auto _aira=$factoryA-getaircondition ();//B Factory Production car$auto _carb=$factoryB-Getauto ();$auto _AIRB=$factoryB-getaircondition ();//Open Audi car + Haier air conditioner$auto _cara-Dirve ();$auto _aira->blow ();//heat can be blown air-conditioning//Open Mercedes-Benz + Air-conditioning blowing gree;$auto _carb-Dirve ();$auto _AIRB->blow ();//Air-conditioning can be blown when hot?>

Factory method Mode:

    • An abstract product class that can derive a number of specific product classes.
    • An abstract factory class that can derive a number of specific factory classes.
    • Each specific factory class can only create an instance of a specific product class.

Abstract Factory mode:

    • Multiple abstract product classes, each abstract product class can derive multiple specific product classes.
    • An abstract factory class that can derive a number of specific factory classes.
    • Each specific factory class can create instances of more than one specific product class.

Simple factory: Used to produce any product in the same grade structure. (inability to add new products)

Factory method: 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)

If you have any questions or wrong place, please leave a message.

PHP design mode (iii) abstract Factory mode (abstracted Factory for PHP)

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.