PHP design model-Abstract factory. PHP design patterns-Abstract factory we have introduced the simple factory and Factory method design patterns. today we are studying the last factory abstraction factory. Case study: catch up with MM and have a meal with the PHP design model -- Abstract factory
We have introduced the simple factory and Factory method design model. today we are studying the last factory-Abstract factory.
Case: try to catch up with MM and go to McDonald's. just say "two B packages" to the waiter. McDonald's is the AbstractFactory of Plan B. Plan B contains hamburgers, chicken wings and drinks. mcDonald's or KFC will let hamburger Factory, chicken wing Factory, and beverage Factory produce materials corresponding to package B according to the specification of Plan B.
Abstract Factory mode: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes. The customer class and the factory class are separated. When a consumer needs a set of products at any time, they only need to request the abstract factory. Abstract factories will then produce products that meet the product set specifications to specific factories.
UML class diagram implementation:
UML class diagram code implementation:
;}}/** Specific product role: Chicken Burger * Class ChickenHamb */class ChickenHamb implements IAllayFood {function Allay () {echo chicken burger .......; }/** Specific product role Coca-Cola * Class KekouKele */class KekouKele implements IDrinkFood {function Drink () {echo Coca-Cola quenching thirst .........; }/** Specific product role Pepsi * Class BaishiKele */class BaishiKele implements IDrinkFood {function Drink () {echo Pepsi quenching thirst ........;} // ------------------- Abstract factory ---------------------/** top-layer super Abstract factory Interface * interface IFactory */Interface IFactory {// get the hunger food function GetAllayFood (); // Get The Thirst Relief food function GetDrinkFood ();}/** factory A package: Shrimp Burger + Pepsi * Class IAFactory */class AFactory implements ifacloud {function GetAllayFood () {return new XiaRenHamb ();} function GetDrinkFood () {return new BaishiKele () ;}/ ** factory B package: chicken burger + Coca-Cola * Class IBFactory */class BFactory implements ifacloud {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. php; // ------------------ point package ------------- $ factoryA = new AFactory (); $ factoryB = new BFactory (); // ------------------ McDonald's food package ------------ // A Package $ allayA = $ factoryA-> GetAllayFood (); $ drinkA = $ factoryA-> GetDrinkFood (); // Package B $ allayB = $ factoryB-> GetAllayFood (); $ drinkB = $ factoryB-> GetDrinkFood (); // ------------------- enjoy package --------------- echo enjoy package :; $ allayA-> Allay (); $ drinkA-> Drink (); echo:; $ allayB-> Allay (); $ drinkB-> Drink ();
When each abstract product has more than one concrete subclass, how does the factory role know which subclass to instantiate? For example, each abstract product role has two specific products. The abstract factory model provides two factory roles, which correspond to the two product roles respectively. each factory role is only responsible for the instantiation of one product role. Each factory class is only responsible for creating instances of a specific subclass of an abstract product.
Applicable scenarios:
1. Multi-style series of scenarios (plans) in game development, such as roads (interfaces), houses, and pipelines.
2. how do you design a system that runs on three different platforms, such as Windows, Linux, and Android? Remove the impact of the operating system on applications by using the abstract Factory mode. Software functions, application logic, and UI on the three different operating systems should be very similar. The only difference is that different factory methods are called, different product categories process the interaction information with the operating system.
3. Abstract Factory mode can be used when the object to be created is a series of interrelated or interdependent product families.
Summary of three factory models:
1. The three forms and features are extremely similar, and the ultimate goal is decoupling. Encapsulate the object creation process so that the client can directly obtain the object without worrying about how to create the object.
2. Comparison
Factory method mode: used to create complex objects. (Single Point food)
Abstract Factory mode: used to create a group of complex objects that are related or mutually dependent. (Buy package)
Generally, there is only one method for creating a Factory method. Abstract factories generally have multiple methods to create a series of products.
We don't have to worry about whether the mode is factory method or abstract Factory mode, because the evolution between them is often unpredictable. The factory method model is clearly used. when a new requirement comes and a slight modification is made, after a new method is added, because the products in the class constitute the product family with different levels of structure, it becomes the abstract factory model. for the abstract factory model, when a method is reduced to make the provided product no longer constitute a product family, it becomes the Factory method model.
Workshop we introduced the simple factory and Factory method design model. today we are studying the last factory Abstract factory. Case: try to catch up with MM...