This article introduces the abstract factory of PHP design pattern, which has certain reference value. Now I share it with you. Friends in need can refer to it
After learning the factory method, the factory method I know is basically to create an object.
For example, when playing a game, I created a Guan Yu hero and a Zhang Fei hero.
But sometimes you may need to create a group of objects during programming, and this leads to the second creational design pattern-- Abstract Factory Method (AbstractFactory).
For example, when I created Guan Yu, I was holding the Qinglong Yanyue Sword and riding a red rabbit horse.
When Liu Bei was created, he held both male and female swords in his hands and rode a horse.
Then their weapons need one class to implement, and the horse needs another class to implement.
Characters, weapons and mounts become a combination.
Finally, the combination creation is achieved through an abstract factory pattern.
The recording code is as follows:
<?php /** Hero * 定义一个英雄接口 */ interface Hero { function CreateHero(); } /** GuanYu * 关羽英雄类 */ Class GuanYu implements Hero { function CreateHero() { echo "关羽加入战场-->"; } } /** LiuBei * 刘备英雄类 */ Class LiuBei implements Hero { function CreateHero() { echo "刘备加入战场-->"; } } /** Weapon * 定义一个兵器接口 */ interface Weapon { function CreateWeapon(); } /** GuanYuWeapon * 青龙偃月刀的类 */ Class GuanYuWeapon implements Weapon { function CreateWeapon() { echo "手持青龙偃月刀-->"; } } /** LiuBeiWeapon * 雌雄双剑的类 */ Class LiuBeiWeapon implements Weapon { function CreateWeapon() { echo "手持雌雄双剑-->"; } } /** Horse * 定义一个坐骑接口 */ interface Horse { function CreateHorse(); } /** ChiTu * 赤兔马类 */ Class ChiTu implements Horse { function CreateHorse() { echo "脚蹬赤兔马。"; } } /** DiLu * 的卢马类 */ Class DiLu implements Horse { function CreateHorse() { echo "脚蹬的卢马。"; } } /** AbstractFactory * 抽象工厂接口 为子类建立标准 * LoadingHero 加载人物类方法 * LoadingWeapon 加载武器类方法 * LoadingHorse 加载坐骑类方法 */ interface AbstractFactory { function LoadingHero(); function LoadingWeapon(); function LoadingHorse(); } /** GFactory * 关羽英雄创建工厂类 用于客户端调用 */ Class GFactory implements AbstractFactory { function LoadingHero() { return new GuanYu(); } function LoadingWeapon() { return new GuanYuWeapon(); } function LoadingHorse() { return new ChiTu(); } } /** LFactory * 刘备英雄创建工厂类 用于客户端调用 */ Class LFactory implements AbstractFactory { function LoadingHero() { return new LiuBei(); } function LoadingWeapon() { return new LiuBeiWeapon(); } function LoadingHorse() { return new DiLu(); } }
<?php // 抽象工厂模式 index.php header("Content-Type:text/html;charset=utf-8"); require_once "AbstractFactory.php"; echo "-----------------创建第一个英雄-----------------<br/>"; $GY = new GFactory(); // 调用第一个英雄的工厂 // 加载各自的创建方法 $GYHero = $GY->LoadingHero(); $GYWeapon = $GY->LoadingWeapon(); $GYHorse = $GY->LoadingHorse(); // 生成加入战场 $GYHero->CreateHero(); $GYWeapon->CreateWeapon(); $GYHorse->CreateHorse(); echo "<br/>"; echo "-----------------创建第二个英雄-----------------<br/>"; $LB = new LFactory(); // 调用第一个英雄的工厂 // 加载各自的创建方法 $LBHero = $LB->LoadingHero(); $LBWeapon = $LB->LoadingWeapon(); $LBHorse = $LB->LoadingHorse(); // 生成加入战场 $LBHero->CreateHero(); $LBWeapon->CreateWeapon(); $LBHorse->CreateHorse();
The output result:
-------------- ---Create the first hero-----------------
Guan Yu joins the battlefield--> Holding the Qinglong Yanyue Sword--> Pedal Red rabbit horse.
-----------------Create a second hero-----------------
Liu Bei joins the battlefield--> holding both male and female swords--> Luma on foot.
Related recommendations:
PHP Design Pattern Factory Method
PHP Design Pattern Simple Factory
The above is the detailed content of PHP design pattern abstract factory. For more information, please follow other related articles on the PHP Chinese website!

如何在PHP后端功能开发中合理应用设计模式?设计模式是一种经过实践证明的解决特定问题的方案模板,可以用于构建可复用的代码,在开发过程中提高可维护性和可扩展性。在PHP后端功能开发中,合理应用设计模式可以帮助我们更好地组织和管理代码,提高代码质量和开发效率。本文将介绍常用的设计模式,并给出相应的PHP代码示例。单例模式(Singleton)单例模式适用于需要保

如何通过编写代码来学习和运用PHP8的设计模式设计模式是软件开发中常用的解决问题的方法论,它可以提高代码的可扩展性、可维护性和重用性。而PHP8作为最新版的PHP语言,也引入了许多新特性和改进,提供更多的工具和功能来支持设计模式的实现。本文将介绍一些常见的设计模式,并通过编写代码来演示在PHP8中如何运用这些设计模式。让我们开始吧!一、单例模式(Sing

本篇文章给大家带来了关于golang设计模式的相关知识,其中主要介绍了职责链模式是什么及其作用价值,还有职责链Go代码的具体实现方法,下面一起来看一下,希望对需要的朋友有所帮助。

随着数据的增长和复杂性的不断提升,ETL(Extract、Transform、Load)已成为数据处理中的重要环节。而Go语言作为一门高效、轻量的编程语言,越来越受到人们的热捧。本文将介绍Go语言中常用的ETL设计模式,以帮助读者更好地进行数据处理。一、Extractor设计模式Extractor是指从源数据中提取数据的组件,常见的有文件读取、数据库读取、A

单例模式是一种常见的设计模式,它在系统中仅允许创建一个实例来控制对某些资源的访问。在 Go 语言中,实现单例模式有多种方式,本篇文章将带你深入掌握 Go 语言中的单例模式实现。

随着JavaScript的不断发展和应用范围的扩大,越来越多的开发人员开始意识到设计模式和最佳实践的重要性。设计模式是一种被证明在某些情况下有用的软件设计解决方案。而最佳实践则是指在编程过程中,我们可以应用的一些最佳的规范和方法。在本文中,我们将探讨JavaScript中的设计模式和最佳实践,并提供一些具体的代码示例。让我们开始吧!一、JavaScript中

设计模式的六大原则:1、单一职责原则,其核心就是控制类的粒度大小、将对象解耦、提高其内聚性;2、开闭原则,可以通过“抽象约束、封装变化”来实现;3、里氏替换原则,主要阐述了有关继承的一些原则;4、依赖倒置原则,降低了客户与实现模块之间的耦合;5、接口隔离原则,是为了约束接口、降低类对接口的依赖性;6、迪米特法则,要求限制软件实体之间通信的宽度和深度。

探索Java开发中的设计模式经验与建议设计模式是软件开发中用于解决特定问题的一种面向对象的可复用解决方案。在Java开发中,设计模式是很重要的一部分,它能够提高代码的可读性和可维护性,并且能够加速开发过程。通过运用设计模式,开发人员可以更好地组织和管理代码,同时也能够避免一些常见的开发错误。在Java开发中,有很多常用的设计模式,如单例模式、工厂模式、观察者


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool