Home > Backend Development > PHP Tutorial > PHP Design Pattern - Factory Method_PHP Tutorial

PHP Design Pattern - Factory Method_PHP Tutorial

WBOY
Release: 2016-07-13 09:56:48
Original
772 people have browsed it

PHP Design Pattern - Factory Method

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.

Factory method pattern The core factory class is no longer responsible for the creation of all products, but leaves the specific creation work to subclasses, becoming an abstract factory role, only responsible for providing the interface that the specific factory class must implement, without touching any A product class should be instantiated with this detail, as shown below:


The factory mode mode mainly consists of the following roles:

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();
    }
}
Copy after login

Test code:

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();
Copy after login

By running the test code in a browser, we can find that the customers all enjoyed their food.

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.




www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/985832.htmlTechArticlePHP Design Pattern - Factory Method Earlier we introduced a simple factory, and today we continue to learn another factory method. Specific case: Ask MM to go to McDonald’s for a burger, different...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template