PHP Design Pattern-Abstract Factory

Source: Internet
Author: User

Statement: This series of blog reference "Big Talk design mode", author Geoscience.


We introduced the simple factory and plant method design pattern, and today we learn the last factory-the abstract factory.


case: chasing mm without eating, to McDonald's, just to the waiter said "two B package" on the line. McDonald's is the B set of abstractfactory,b package containing hamburgers, chicken wings and drinks. McDonald's or KFC will make burger factory, chicken wings factory, and beverage factory according to the specifications of the B package, respectively, to produce the material corresponding to the B package.

Abstract Factory mode: Provides an interface to create a series of related or interdependent objects without specifying their specific classes. The customer class and the factory class are separated. When consumers need a set of products at any time, they simply request to the abstract factory. The abstract factory will then produce products in accordance with the product set specifications to the specific factory.


UML class diagram implementations:


UML Class Diagram code implementation:

<?php/** * Created by Phpstorm. * user:lyl * DATE:2015/4/19 * time:17:39 *///-----------------------Products------------------------/** Abstract product role Hunger food Object * Interface Iallayfood */interface iallayfood{function Allay ();} /** Abstract Product role Thirst quenching food * Interface idrinkfood */interface idrinkfood{function Drink ();}  /** Specific Product Roles Shrimp Burger * Class xiarenhamb */class Xiarenhamb implements iallayfood{function allay () {echo "Shrimp burger is a hunger ....    <br/> ";        }}/** Specific Product Roles Chicken Burger * Class chickenhamb */class Chickenhamb implements iallayfood{function allay () { echo "Chicken burger is a hunger ....    <br/> "; }}/** specific product roles Coca-Cola * Class Kekoukele */class Kekoukele implements idrinkfood{function Drink () {EC Ho "Coca-Cola quench the thirst ....    <br/> ";        }}/** Specific Product Roles Pepsi * Class Baishikele */class Baishikele implements idrinkfood{function Drink () { Echo "Pepsi quench the thirst ....    <br/> "; }}//-------------------Abstract Factory---------------------/** top layer Super Abstract Factory interface * Interface ifactory */interface ifactory{//Get hunger Food function getallayfood (); Get quench thirst food function getdrinkfood ();}        /** factory A A package: Shrimp Burger + pepsi * Class iafactory */class afactory implements ifactory{function Getallayfood () {    return new Xiarenhamb ();    } function Getdrinkfood () {return new Baishikele ();    }}/** Plant b b Package: Chicken Burger + Coca-Cola * Class ibfactory */class bfactory implements ifactory{function Getallayfood ()    {return new chickenhamb ();    } function Getdrinkfood () {return new Kekoukele (); }}

Client Test Code

Header ("Content-type:text/html;charset=utf-8");//------------------------Abstract Factory test code------------------require_ Once "./abstractfactory/abstractfactory.php";//-------------------point package-------------$factoryA =new afactory (); $ Factoryb=new bfactory ()//------------------McDonald's production package food------------//a set $allaya= $factoryA->getallayfood (); $ Drinka= $factoryA->getdrinkfood ();//b set $allayb= $factoryB->getallayfood (); $drinkB = $factoryB Getdrinkfood ()///-------------------Enjoy the package---------------echo "Enjoy a set:<br/>"; $allayA->allay (); $drinkA- >drink (); echo "Enjoy the B set:<br/>"; $allayB->allay (); $drinkB->drink ();

When each abstract product has more than one specific subclass, how does the factory role know which subclass to instantiate? For example, each abstract product role has two specific products. Abstract Factory mode provides two specific factory roles that correspond to these two specific product roles, each of which is responsible for instantiating only one product role. Each specific factory class is only responsible for creating an instance of a specific subclass of the abstract product .


Applicable scenarios:

1, game development in the multi-style series of scenes (packages), such as roads (interfaces), houses, pipelines and so on.

2, the system to run on three different platforms, such as Windows, Linux, Android run, how would you design? The effect of the operating system on the application is masked by the abstract Factory mode. The software features, application logic, and UI on three different operating systems should be very similar, the only difference being that different factory methods are called, and the different product classes handle the information that interacts with the operating system.

3. Abstract Factory mode can be used when the object that needs to be created is a series of interrelated or interdependent product families.


Summary of three factory models:

1. Three species are very similar in form and character, and the ultimate goal is decoupling. Encapsulates the creation of an object so that the client can directly get the object without having to care about how to create the object.

2. Contrast

Factory method Mode: Used to create complex objects. (à la carte food)

Abstract Factory mode: Used to create a set of related or interdependent complex objects. (Buy package)

Factory method Creation There is generally only one way to create a product. Abstract factories generally have multiple methods to create a series of products.


We don't have to worry about whether the patterns are factory-mode or abstract factory-mode, because their evolution is often elusive. Clearly used factory method mode, when the new requirements come, a little modification, adding a new method, because the products in the class constitute a different hierarchical structure of the product family, it becomes an abstract Factory mode, and for the abstract factory model, when the reduction of a method of the provided products no longer constitute the product family, It evolved into a factory method model.


PHP Object-oriented design pattern

PHP Design Pattern-Abstract Factory

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.