search
HomeBackend DevelopmentPHP TutorialPHP design pattern simple factory pattern
PHP design pattern simple factory patternJul 30, 2020 pm 03:51 PM
php design patternsSimple 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();  // 开始播放

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
如何在PHP中应用简单工厂模式来提高代码的复用性如何在PHP中应用简单工厂模式来提高代码的复用性Sep 05, 2023 pm 12:27 PM

如何在PHP中应用简单工厂模式来提高代码的复用性简单工厂模式(SimpleFactoryPattern)是一种常用的设计模式,可以在创建对象时提供一种统一的接口,以便根据不同的条件来创建不同的实例。这种模式可以有效地降低代码的耦合度,提高代码的可维护性和复用性。在PHP中,我们可以利用简单工厂模式来优化代码的结构和逻辑。理解简单工厂模式简单工厂模式由三个

如何通过PHP面向对象简单工厂模式实现对象的版本控制和管理如何通过PHP面向对象简单工厂模式实现对象的版本控制和管理Sep 06, 2023 pm 02:39 PM

如何通过PHP面向对象简单工厂模式实现对象的版本控制和管理在开发大型的、复杂的PHP项目时,版本控制和管理是非常重要的一环。通过适当的设计模式,我们可以更好地管理和控制对象的创建和使用,从而提高代码的可维护性和扩展性。本文将介绍如何使用PHP面向对象简单工厂模式来实现对象的版本控制和管理。简单工厂模式是一种创建类的设计模式,它通过一个工厂类来实例化指定的对象

如何在PHP中应用简单工厂模式来实现对象的自动化创建如何在PHP中应用简单工厂模式来实现对象的自动化创建Sep 05, 2023 pm 02:27 PM

如何在PHP中应用简单工厂模式来实现对象的自动化创建简单工厂模式是一种常见的设计模式,它用于创建对象并抽象了实例化对象的过程。在PHP中,应用简单工厂模式可以帮助我们将对象的创建和具体实现解耦,使代码更加灵活和可维护。在本文中,我们将使用一个示例来说明如何在PHP中应用简单工厂模式。假设我们有一个电子产品店,它销售手机和电视机。我们需要根据用户的选择来创建相

PHP中常用的设计模式及其实现方法PHP中常用的设计模式及其实现方法Jun 27, 2023 pm 01:08 PM

PHP是一种广泛使用且非常流行的编程语言。当今的Web应用程序中,PHP是非常重要的一部分。在开发PHP应用程序的过程中,设计模式起着至关重要的作用。设计模式是解决问题的一种模板,在不同的环境中可重复使用,帮助我们写出更好的代码,使代码更加可靠、可维护、可扩展。在本文中,我们将探讨一些PHP中常用的设计模式及其实现方法。单例模式单例模式是一种创建模式,它允许

如何通过PHP面向对象简单工厂模式实现对象的多态性如何通过PHP面向对象简单工厂模式实现对象的多态性Sep 05, 2023 am 08:43 AM

如何通过PHP面向对象简单工厂模式实现对象的多态性简单工厂模式是一种常见的设计模式,它可以通过一个共同的工厂类来创建不同类的对象,并且隐藏了对象的创建过程。PHP面向对象简单工厂模式可以帮助我们实现对象的多态性。简单工厂模式包含三个基本角色:工厂类、抽象类和具体类。首先我们来定义一个抽象类Animal,它包含一个抽象方法say():abstractclas

深入探讨Java工厂模式的实现与应用深入探讨Java工厂模式的实现与应用Feb 24, 2024 pm 10:15 PM

Java工厂模式的原理与应用详解工厂模式是一种常用的设计模式,它用于创建对象,以及将对象的创建过程封装起来。Java中的工厂模式有多种实现方式,其中最常见的有简单工厂模式、工厂方法模式和抽象工厂模式。本文将详细介绍这三种工厂模式的原理和应用,并给出相应的代码示例。一、简单工厂模式简单工厂模式是最简单、最常用的工厂模式。它通过一个工厂类,根据传入的参数来返回不

php有哪些设计模式php有哪些设计模式Jul 25, 2023 am 09:39 AM

php设计模式有:1、单例模式,确保一个类只有一个实例化对象;2、工厂模式,将对象的实例化过程封装在一个工厂类中;3、抽象工厂模式,是一种类似于工厂模式的创建对象的模式;4、观察者模式,实现对象之间的一对多依赖关系;5、适配器模式,将一个类的接口转换成另一个类的接口;6、装饰器模式,动态地给一个对象添加一些额外的功能;7、迭代器模式;8、策略模式;9、模板方法模式等等。

如何使用PHP面向对象简单工厂模式创建可测试的对象实例如何使用PHP面向对象简单工厂模式创建可测试的对象实例Sep 05, 2023 pm 02:45 PM

如何使用PHP面向对象简单工厂模式创建可测试的对象实例简单工厂模式是一种常用的软件设计模式,它可以帮助我们根据不同的条件创建不同的对象实例。在PHP面向对象编程中,结合简单工厂模式可以提高代码的可测试性和可维护性。在本文中,我们将学习如何使用PHP面向对象简单工厂模式创建可测试的对象实例。我们将以一个简单的示例来说明这个过程。首先,让我们定义一个接口来表示要

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.