PHP design model-factory Method
We introduced a simple factory. Today we continue to learn about another factory-factory method.
Specific case: Ask MM to go to McDonald's for a hamburger. Different MM has different tastes. It is annoying to remember every one. We generally adopt the FactoryMethod mode, take the MM to the waiter and say "I want a hamburger". What kind of hamburger should I ask the MM to tell the waiter directly.
Factory method mode the core factory category is no longer responsible for the creation of all products, but instead is responsible for handing over the specific creation work to sub-classes to become an abstract factory role, it is only responsible for providing the interface that must be implemented by a specific factory class, without touching the details of which product class should be instantiated, such:
The factory mode consists of the following roles:
Abstract Factory role (IServerFactory): it is the core of the factory method mode and has nothing to do with the application. Any factory class of the object created in the mode must implement this interface.
ChickenLegBaoFactory: This is a factory class that implements Abstract Factory interfaces. It contains the logic closely related to the application and is called by the application to create product objects.
Abstract Product role (IHanbao): the super type of the object created in the factory method mode, that is, the common parent class of the product object or the common interfaces. In, this role is Light.
Specific product role (ChickenLegBao): this role implements the interface defined by the abstract Product role. A specific product is created in a specific factory, which is usually one-to-one.
Compile the following PHP code based on the above UML class diagram and role:
;}}/** Chicken burger * Class ChickenBao */class ChickenBao implements IHanbao {function Allay () {echo I am chicken burger, the little one is starving the master !; }/** Abstract Factory role * Interface IServerFactory */interface IServerFactory {function MakeHanbao ();} /** the specific factory role of the meat pine hamburger factory * Class RouSongFactory */class RouSongFactory implements IServerFactory {function MakeHanbao () {return new RouSongBao ();}} class ChickenFactory implements IServerFactory {function MakeHanbao () {return new ChickenBao ();}}
Test code:
Header (Content-Type: text/html; charset = UTF-8); // ------------------------ factory-based test code ------------------ require_once. /FactoryMethod. php; // ----------------- restaurant cook ----------- $ chickenFactory = new ChickenFactory (); $ rouSongFactory = new RouSongFactory (); // ----------- order ------------ $ chicken1 = $ chickenFactory-> MakeHanbao (); $ chicken2 = $ chickenFactory-> MakeHanbao (); $ rouSong1 = $ scheme-> MakeHanbao (); $ rouSong2 = $ rouSongFactory-> MakeHanbao (); // ------------------ customers eat --------- $ chicken1-> Allay (); $ chicken2-> Allay (); $ rouSong1-> Allay (); $ rouSong2-> Allay ();
Run the test code in a browser and we can find that customers enjoy their food.
Advantages and disadvantages of factory method mode:
Advantage: the simple factory mode violates the open-closed principle and maintains the advantages of the encapsulated object creation process.
Defect: when you add a product, you need to add a product factory class to increase the additional development workload. Branch judgment cannot be avoided.
Comparison between simple factory mode and factory method mode:
1. Structure Complexity
The simple factory mode must be dominant. The simple factory mode only requires one factory class, while the factory class of the factory method mode increases with the number of product classes, thus increasing the complexity of the structure.
2. Code complexity
There is a conflict between Code complexity and Structure Complexity. Since the simple factory mode is relatively simple in structure, it must be more complex in code than the factory method mode. The factory class in the simple factory mode needs to add many methods (or Code) as the product class increases, while the factory method mode only completes a single task for each specific factory class, and the code is concise.
3. management difficulty
If a specific product class needs to be modified, it is likely that the corresponding factory class needs to be modified. When you need to modify multiple product classes at the same time, modification to the factory class will become quite troublesome. The simple factory does not have these troubles. When multiple product classes need to be modified, the simple factory model still only needs to modify the unique factory class.