Understand simple factories, factory methods, and abstract factories in one article

齐天大圣
Release: 2023-04-09 12:06:01
Original
1911 people have browsed it

Simple Factory Mode

Basically everyone has a music player in their mobile phone. The currently popular players are: QQ Music, Kugou Music , Sogou Music, NetEase Cloud Music, Tiantian Dongting, etc. The following is a piece of code about playing music:

if ($type == 'QQ') {
    $player = new QQPlayer();
} else if ($type == 'Wy') {
    $player = new WyPlayer();
} else if ($type == 'KG') {
    $player = new KGPlayer();
} else {
    $palyer = null;
}
 
$player->on();  // 打开播放器
$player->choiceMusic('我不配');  // 选择歌曲
$player->play();  // 开始播放
Copy after login

In order to make the code logic clearer and more readable, we must be good at encapsulating functionally independent code blocks into functions. According to this design idea, we can extract the conditional branches and place them in a separate method in a class. This class can be called the simple factory pattern.

Definition of simple factory pattern: A class can obtain different instances based on different parameters. Generally, these created instances have the same parent class.

Static factory pattern: Generally, we set the methods used to create different instances in the simple factory pattern as static methods to avoid creating multiple identical instances.

Let’s rewrite the above code using the simple factory pattern

class MusicPlayerFactory
{
    public static function create ($type)
    {
        if ($type == 'QQ') {
            $player = new QQPlayer();
        } else if ($type == 'Wy') {
            $player = new WyPlayer();
        } else if ($type == 'KG') {
            $player = new KGPlayer();
        } else {
            $player = null;
        }
        return $player;
    }
}

// 业务代码修改如下
$player = MusicPlayerFactory:create('QQ');
$player->on();  // 打开播放器
$player->choiceMusic('我不配');  // 选择歌曲
$player->play();  // 开始播放
Copy after login

For the above simple factory pattern, if we need to add a new music player, we will definitely modify it The create method of MusicPlayerFactory is somewhat inconsistent with the "opening and closing principle". There are not many such conditional branches, and the creation of other classes is also very simple. It is completely possible to use the simple factory pattern. If you have to remove the if branch logic and make it comply with the "opening and closing principle", then you can use the factory method to achieve it. For factory methods, it is not necessarily better than the simple factory pattern. Although its scalability is better, it sacrifices readability.

Factory method pattern

Definition: In the factory method pattern, the factory parent class is responsible for defining the public interface for creating product objects, and the factory child The class is responsible for generating specific product objects. The purpose of this is to delay the instantiation operation of the product class to the factory subclass, that is, the factory subclass is used to determine which specific product class should be instantiated.

Now we use "polymorphism" to eliminate the if branch structure of the simple factory pattern above. The implemented code is as follows:

interface IMusicPlayerFactory
{
    static function create ();
}

class QQPlayerFactory implements IMusicPlayerFactory
{
    public static function create ()
    {
        return new QQPlayer();
    }
}

class WyPlayerFactory implements IMusicPlayerFactory
{
    public static function create ()
    {
        return new WyPlayer();
    }
}

class KGPlayerFactory implements IMusicPlayerFactory
{
    public static function create ()
    {
        return new KGPlayer();
    }
}

// 业务代码修改如下
if ($type == 'QQ') {
    $player = QQPlayerFactory::create();
} else if ($type == 'Wy') {
    $player = WyPlayerFactory::create();
} else if ($type == 'KG') {
    $player = KGPlayerFactory::create();
} else {
    throw new \Exception('...');
}
$player->on();  // 打开播放器
$player->choiceMusic('我不配');  // 选择歌曲
$player->play();  // 开始播放
Copy after login

As you can see, the problem has returned to the original point, and the if conditional branch structure appears again in the business code. So how to solve this problem?

We can create a simple factory for the factory class to create factory class objects. The new simple factory code is as follows:

class MusicPlayerFactoryMap
{
    const Players = [
        'QQ' => 'QQPlayerFactory',
        'Wy' => 'WyPlayerFactory',
        'KG' => 'KGPlayerFactory'
    ];
    public static function getPlayerFactory (string $type)
    {
        if (empty($type)) {
            return null;
        }
        return (self::Players[$type])::create();
    }
}

// 业务代码修改如下
$palyer = MusicPlayerFactoryMap::getPlayerFactory('QQ')
$player->on();  // 打开播放器
$player->choiceMusic('我不配');  // 选择歌曲
$player->play();  // 开始播放
Copy after login

As you can see, using the factory pattern, the structure becomes much more complex than before. We only recommend using the factory method pattern if the process of creating an instance is copied.

Abstract Factory Pattern

The abstract factory pattern is used in special scenarios and is rarely used. In the factory method pattern, specific factories are responsible for producing specific products, and each factory corresponds to a specific product. But sometimes, we need a factory that can create multiple product objects instead of a single product.

Let’s take a look at an example: The target computer factory is responsible for producing computers for sale. We know that a computer is composed of a host, a keyboard, a monitor and a mouse. Currently, the Target Computer City only produces three types of computers, low-profile, mid-range and high-profile. Computers with different configurations use different host brands, monitor brands, etc. of.

The hosts currently include: Kirin host, Thunder host, Winter host

The keyboards currently include: Rapoo, Logitech, Razer

The monitors currently include: aoc, hkc, BenQ

Mouses currently include: Logitech, Spirit Snake, and Founder

The top version of the computer is composed of Kirin host, Rapoo keyboard, AOC monitor, and Logitech mouse, and the mid-range version is composed of...

The code for the host is as follows:

interface Host
{
    static function createHost ();
}
class DrHost implements Host
{
    public static function createHost()
    {
        echo '创建冬日主机' . PHP_EOL;
    }
}
class QlHost implements Host
{
    public static function createHost()
    {
        echo '创建麒麟主机' . PHP_EOL;
    }
}
class LtHost implements Host
{
    public static function createHost()
    {
        echo '创建雷霆主机' . PHP_EOL;
    }
}
Copy after login

Similarly, to create a keyboard, monitor, and mouse, the code will not be posted here.

Now, we define an interface for creating computers.

interface ComputerFactory
{
    static function createHost ();
    static function createKeyboard ();
    static function createMonitor ();
    static function createMouse ();
}
Copy after login

Then complete three specific factories for creating low-end, mid-end and high-end computers.

class GreatComputerFactory implements ComputerFactory
{
    public static function createHost()
    {
        QlHost::createHost();
    }
    public static function createKeyboard()
    {
        LbKeyboard::createKeyboard();
    }
    public static function createMonitor()
    {
        AocMonitor::createMonitor();
    }
    public static function createMouse()
    {
        LjMouse::createMouse();
    }
}
class GoodComputerFactory implements ComputerFactory
{
    public static function createHost()
    {
        LtHost::createHost();
    }
    public static function createKeyboard()
    {
        LjKeyboard::createKeyboard();
    }
    public static function createMonitor()
    {
        HkcMonitor::createMonitor();
    }
    public static function createMouse()
    {
        LsMouse::createMouse();
    }
}
class NormalComputerFactory implements ComputerFactory
{
    public static function createHost()
    {
        DrHost::createHost();
    }
    public static function createKeyboard()
    {
        LsKeyboard::createKeyboard();
    }
    public static function createMonitor()
    {
        BenqMonitor::createMonitor();
    }
    public static function createMouse()
    {
        FzMouse::createMouse();
    }
}
Copy after login

Now you can create a specific computer

class GreatComputer
{
    public function __construct()
    {
        echo '高配电脑' . PHP_EOL;
        GreatComputerFactory::createHost();
        GreatComputerFactory::createKeyboard();
        GreatComputerFactory::createMonitor();
        GreatComputerFactory::createMouse();
    }
}
class GoodComputer
{
    public function __construct()
    {
        echo '中配电脑' . PHP_EOL;
        GoodComputerFactory::createHost();
        GoodComputerFactory::createKeyboard();
        GoodComputerFactory::createMonitor();
        GoodComputerFactory::createMouse();
    }
}
class NormalComputer
{
    public function __construct()
    {
        echo '低配电脑' . PHP_EOL;
        NormalComputerFactory::createHost();
        NormalComputerFactory::createKeyboard();
        NormalComputerFactory::createMonitor();
        NormalComputerFactory::createMouse();
    }
}
Copy after login

The above is the detailed content of Understand simple factories, factory methods, and abstract factories in one article. For more information, please follow other related articles on the PHP Chinese website!

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