We introduced a simple factory earlier, and today we continue to learn another factory-factory method.
Specific case: Ask MM to go to McDonald's to eat hamburgers. Different MMs have different tastes. It is annoying to remember each one. We generally use the FactoryMethod mode and take MM to the waiter and say "I want a hamburger." , specifically what kind of burger you want, just ask MM to tell the waiter directly.
Abstract factory role (IServerFactory): It is the core of the factory method pattern and has nothing to do with the application. Any factory class for objects created in the pattern must implement this interface.
Concrete factory role (ChickenLegBaoFactory): This is a concrete factory class that implements the abstract factory interface, contains 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 by the factory method pattern, which is the common parent class or commonly owned interface of the product object. In the picture above, this character is Light.
Concrete product role (ChickenLegBao): This role implements the interface defined by the abstract product role. A specific product is created in a specific factory, and there is often a one-to-one correspondence between them.
Write the following PHP code based on the above UML class diagram and roles:
<!--?php /** * Created by PhpStorm. * User: Jiang * Date: 2015/4/16 * Time: 22:12 */ /**抽象产品角色 汉堡 * Interface IHanbao */ interface IHanbao { function Allay(); } /**具体产品角色 肉松汉堡 * Class RouSongBao */ class RouSongBao implements IHanbao { function Allay() { echo I am 肉松汉堡,小的给主人解饿了!<br/-->; } } /**鸡肉汉堡 * Class ChickenBao */ class ChickenBao implements IHanbao { function Allay() { echo I am 鸡肉汉堡,小的给主人解饿了! ; } } /**抽象工厂角色 * Interface IServerFactory */ interface IServerFactory { function MakeHanbao(); } /**具体工厂角色 肉松汉堡工厂 * Class RouSongFactory */ class RouSongFactory implements IServerFactory { function MakeHanbao() { return new RouSongBao(); } } class ChickenFactory implements IServerFactory { function MakeHanbao() { return new ChickenBao(); } }
header(Content-Type:text/html;charset=utf-8); //------------------------工厂方式测试代码------------------ require_once ./FactoryMethod/FactoryMethod.php; //-----------------餐厅厨师----------- $chickenFactory=new ChickenFactory(); $rouSongFactory=new RouSongFactory(); //-----------点餐------------ $chicken1=$chickenFactory->MakeHanbao(); $chicken2=$chickenFactory->MakeHanbao(); $rouSong1=$rouSongFactory->MakeHanbao(); $rouSong2=$rouSongFactory->MakeHanbao(); //------------------顾客吃饭--------- $chicken1->Allay(); $chicken2->Allay(); $rouSong1->Allay(); $rouSong2->Allay();
Advantages and Disadvantages of Factory Method Pattern:
Advantages: It overcomes the violation of the open-closed principle of the simple factory model and maintains the advantages of encapsulating the object creation process.
Disadvantage: When adding products, you have to add a product factory class, which increases the amount of additional development. The problem of branch judgment cannot be avoided.
Comparison of simple factory pattern and factory method pattern:
1. Structural complexity
The simple factory model will prevail. The simple factory pattern only requires one factory class, while the factory classes of the factory method pattern increase as the number of product classes increases, thus increasing the complexity of the structure.2. Code complexity
Code complexity and structural complexity are a contradiction. Since the simple factory pattern is relatively simple in terms of structure, it must be more complex than the factory method pattern in terms of code. The factory class of the simple factory pattern needs to add many methods (or code) as the product category increases, while in the factory method pattern, each specific factory class only completes a single task 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 multiple product classes need to be modified at the same time, modifying the factory class will become quite troublesome. On the contrary, simple factories do not have these troubles. When multiple product classes need to be modified, the simple factory pattern still only needs to modify the only factory class.