Home Backend Development PHP Tutorial PHP design pattern simple factory pattern

PHP design pattern simple factory pattern

Jul 30, 2020 pm 03:51 PM
php design patterns Simple factory pattern

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();  // 开始播放
Copy after login

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

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

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

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Commonly used design patterns in PHP and their implementation methods Commonly used design patterns in PHP and their implementation methods Jun 27, 2023 pm 01:08 PM

PHP is a widely used and very popular programming language. PHP is a very important part of today's web applications. Design patterns play a vital role in developing PHP applications. Design pattern is a template for solving problems that can be reused in different environments. It helps us write better code and make the code more reliable, maintainable and scalable. In this article, we will explore some commonly used design patterns in PHP and how to implement them. Singleton Pattern Singleton pattern is a creation pattern that allows

How to apply the simple factory pattern in PHP to improve code reusability How to apply the simple factory pattern in PHP to improve code reusability Sep 05, 2023 pm 12:27 PM

How to apply the simple factory pattern in PHP to improve code reusability. The simple factory pattern (SimpleFactoryPattern) is a commonly used design pattern that can provide a unified interface when creating objects so that different objects can be created according to different conditions. Example. This mode can effectively reduce the coupling of code and improve the maintainability and reusability of code. In PHP, we can use the simple factory pattern to optimize the structure and logic of the code. Understanding the simple factory pattern The simple factory pattern consists of three

How to implement object version control and management through PHP object-oriented simple factory pattern How to implement object version control and management through PHP object-oriented simple factory pattern Sep 06, 2023 pm 02:39 PM

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

Research on three design methods of Java factory pattern Research on three design methods of Java factory pattern Feb 18, 2024 pm 05:16 PM

Explore Three Design Ideas of Java Factory Pattern Factory pattern is a commonly used design pattern for creating objects without specifying a specific class. In Java, the factory pattern can be implemented in many ways. This article will explore the implementation of three Java factory patterns based on different design ideas and give specific code examples. Simple Factory Pattern The simple factory pattern is the most basic factory pattern, which creates objects through a factory class. The factory class determines what kind of specific object should be created based on the client's request parameters. Below is a brief

How to create testable object instances using PHP object-oriented simple factory pattern How to create testable object instances using PHP object-oriented simple factory pattern Sep 05, 2023 pm 02:45 PM

How to use the PHP object-oriented simple factory pattern to create testable object instances. The simple factory pattern is a commonly used software design pattern that helps us create different object instances based on different conditions. In PHP object-oriented programming, combining the simple factory pattern can improve the testability and maintainability of the code. In this article, we will learn how to create testable object instances using the object-oriented simple factory pattern in PHP. We will illustrate this process with a simple example. First, let's define an interface to represent the

In-depth discussion on the implementation and application of Java factory pattern In-depth discussion on the implementation and application of Java factory pattern Feb 24, 2024 pm 10:15 PM

Detailed explanation of the principles and applications of Java factory pattern Factory pattern is a commonly used design pattern, which is used to create objects and encapsulate the object creation process. There are many ways to implement the factory pattern in Java, the most common of which are the simple factory pattern, the factory method pattern and the abstract factory pattern. This article will introduce the principles and applications of these three factory patterns in detail, and give corresponding code examples. 1. Simple Factory Pattern The simple factory pattern is the simplest and most commonly used factory pattern. It uses a factory class to return different values ​​based on the parameters passed in.

How to apply the simple factory pattern in PHP to realize automated creation of objects How to apply the simple factory pattern in PHP to realize automated creation of objects Sep 05, 2023 pm 02:27 PM

How to apply the simple factory pattern in PHP to automate object creation. The simple factory pattern is a common design pattern that is used to create objects and abstracts the process of instantiating objects. In PHP, applying the simple factory pattern can help us decouple object creation and specific implementation, making the code more flexible and maintainable. In this article, we will use an example to illustrate how to apply the Simple Factory Pattern in PHP. Suppose we have an electronics store that sells mobile phones and televisions. We need to create photos based on user selections

How to achieve object polymorphism through PHP object-oriented simple factory pattern How to achieve object polymorphism through PHP object-oriented simple factory pattern Sep 05, 2023 am 08:43 AM

How to achieve object polymorphism through the PHP object-oriented simple factory pattern. The simple factory pattern is a common design pattern that can create objects of different types through a common factory class and hide the object creation process. PHP object-oriented simple factory pattern can help us achieve object polymorphism. The simple factory pattern contains three basic roles: factory class, abstract class and concrete class. First we define an abstract class Animal, which contains an abstract method say(): abstractclas

See all articles