PHP design Mode-factory method _php Tutorial

Source: Internet
Author: User

PHP design mode-factory method


Before we introduced the simple factory, today we continue to learn another factory-the factory method.

Specific case: Please mm to McDonald's to eat hamburgers, different mm have different tastes, to each remember is a annoying thing, we generally use FactoryMethod mode, with mm to the waiter there, said "to a hamburger", specifically what kind of hamburger, let mm directly with the waiter said on the line.

Factory method mode The core factory class is no longer responsible for the creation of all products, but rather the creation of the work to the subclass to do, as an abstract factory role, is only responsible for the specific factory class must be implemented interface, and do not touch which product class should be instantiated such details, such as:


The Factory mode pattern is mainly composed of the following types of roles:

Abstract Factory Role (Iserverfactory): is the core of the factory method pattern, independent of the application. Any factory class of objects created in the schema must implement this interface.

Specific factory Role (Chickenlegbaofactory): This is the specific factory class that implements the abstract factory interface, contains logic that is closely related to the application, and is called by the application to create the Product object.

Abstract product Role (Ihanbao): The super-type of the object created by the factory method pattern, which is the common parent or co-owned interface of the product object. In, this role is light.

Specific product role (CHICKENLEGBAO): This role implements the interface defined by the abstract product role. A specific product has a specific factory-created, often one by one correspondence between them.

Write the following PHP code based on the UML class diagrams and roles above:

 
  ;    }} /** Chicken Burger * Class Chickenbao */class Chickenbao implements ihanbao{    function allay ()    {        echo I am Chicken burger, small to the owner starvation! ;    }} /** Abstract Factory Role * Interface iserverfactory */interface iserverfactory{    function Makehanbao ();} /** Specific factory role     meat floss Hamburg 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 mode test code------------------require_once. /factorymethod/factorymethod.php;//-----------------Restaurant chef-----------$chickenFactory =new chickenfactory (); $ Rousongfactory=new rousongfactory ()//-----------order------------$chicken 1= $chickenFactory->makehanbao (); $ chicken2= $chickenFactory->makehanbao (), $rouSong 1= $rouSongFactory->makehanbao (); $rouSong 2=$ Rousongfactory->makehanbao ()//------------------customer---------$chicken 1->allay (); $chicken 2->allay (); $ Rousong1->allay (); $rouSong 2->allay ();

Run the test code in a browser and we can see that the customers are enjoying their own food.

Advantages and disadvantages of the factory method model:

Advantage: Overcoming the Simple factory model violates the principle of open-closed, preserving the advantages of the encapsulation object creation process.

BUG: When adding a product, you have to add a product factory class and add additional development. The problem of branch judgment cannot be avoided.

Comparison of simple Factory mode with factory method mode:

1. Complexity of structure

The simple factory model should be superior. The simple factory model requires only one factory class, while the factory-mode factory class increases as the number of product classes increases, thus increasing the complexity of the structure.

2. Complexity of code

Code complexity and structural complexity are a pair of contradictions, since the simple factory model is relatively concise in terms of structure, then it is certainly more complex in code than the factory method pattern. Factory classes in simple Factory mode as the product class increases, many methods (or code) are added, while the factory method pattern accomplishes only a single task for each specific factory class, and the code is concise.

3. Difficulty in Management

If a specific product class needs to be modified, it may be necessary to modify the corresponding factory class. When you need to modify multiple product classes at the same time, modifications to the factory class can become quite cumbersome. Instead of a simple factory without these troubles, when multiple product classes need to be modified, the simple factory model still needs only to modify the unique factory class.




http://www.bkjia.com/PHPjc/985832.html www.bkjia.com true http://www.bkjia.com/PHPjc/985832.html techarticle PHP Design Mode-factory method we introduced the simple factory, and today we continue to learn another factory factory method. Specific case: Please mm to McDonald's to eat hamburgers, different ...

  • 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.