Home  >  Article  >  Backend Development  >  PHP design pattern simple factory pattern

PHP design pattern simple factory pattern

齐天大圣
齐天大圣Original
2020-07-30 15:51:221861browse

Listen to music

The mainstream music players currently on the market include QQ Music, NetEase Cloud Music, Kugou Music, Kuwo Music, etc.

PHP design pattern simple factory pattern

Different people may use different players to listen to music. We may choose one of the music players to listen to music, please see the code below:

if ($type == 1) {
    $player = new QQPlayer();
} else if ($type == 2) {
    $player = new WyPlayer();
} else if ($type == 3) {
    $player = new KGPlayer();
}

$player->on();  // 打开播放器
$player->choiceMusic('思念是一种病');  // 选择歌曲
$player->play();  // 开始播放

If at this time, we want to add Kuwo Music Player or think we can eliminate NetEase Cloud Music (cannot listen to it) Jay Chou's song), then the program needs to be modified (the conditional branch needs to be modified). Therefore, such code is difficult to maintain.

One principle when writing programs is to extract and encapsulate the changes. We extract the conditional branch section and encapsulate it into a new class.

Simple Engineering Mode

Definition: Define a factory class that can return instances of different classes based on different parameters. The created instances usually have a common parent class

The method used to create the instance in the simple factory pattern is usually a static method, so the simple factory pattern is also calledstatic factory method

Next, let’s create a simple engineering mode, the code is as follows:

class SimpleFactory
{
    public static function createMusicPlayer ($type)
    {
        if ($type == 1) {
            $player = new QQPlayer();
        } else if ($type == 2) {
            $player = new WyPlayer();
        } else if ($type == 3) {
            $player = new KGPlayer();
        } else {
            return null;
        }
        return $player;
    }
}

Then, the code at the beginning of the article can be modified as follows:

$player  = SimpleFactory::createMusicPlayer($type);

$player->on();  // 打开播放器
$player->choiceMusic('思念是一种病');  // 选择歌曲
$player->play();  // 开始播放

You may have questions, what is the use of doing this? It seems to just move the problem from one place to another.

Think about it, we may listen to music in the morning or at night, or if this player does not have the music we want to listen to, we need to change to another player. Then it is possible to create new music players in multiple places. So it is better to put this code in one place than in multiple places. When you need to change it, you only need to put it in one place. When maintaining this way, you only need to modify createMusicPlayer in the simple factory pattern class.

The above is the detailed content of PHP design pattern simple factory pattern. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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