File tree Expand file tree Collapse file tree 7 files changed +85
-4
lines changed
Expand file tree Collapse file tree 7 files changed +85
-4
lines changed Original file line number Diff line number Diff line change 1- # PHP 设计模式系列 —— 抽象工厂模式(Abstract Factory)
1+ # PHP 设计模式 —— 抽象工厂模式(Abstract Factory)
22## 1、模式概述
33抽象工厂模式为一组相关或相互依赖的对象创建提供接口,而无需指定其具体实现类。抽象工厂的客户端不关心如何创建这些对象,只关心如何将它们组合到一起。
44## 2、问题引出
Original file line number Diff line number Diff line change 1- # PHP 设计模式系列 —— 建造者模式(Builder)
1+ # PHP 设计模式 —— 建造者模式(Builder)
22## 1、模式定义
33建造者模式将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
44
Original file line number Diff line number Diff line change 1- # PHP 设计模式系列 —— 依赖注入模式(Dependency Injection)
1+ # PHP 设计模式 —— 依赖注入模式(Dependency Injection)
22## 1、模式定义
33依赖注入(Dependency Injection)是控制反转(Inversion of Control)的一种实现方式。
44
Original file line number Diff line number Diff line change 1- # PHP 设计模式系列 —— 工厂方法模式(Factory Method)
1+ # PHP 设计模式 —— 工厂方法模式(Factory Method)
22## 1、模式定义
33定义一个创建对象的接口,但是让子类去实例化具体类。工厂方法模式让类的实例化延迟到子类中。
44
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: chenchangqin
5+ * Date: 2018/12/19
6+ * Time: 17:19
7+ */
8+
9+ namespace DesignerPatterns \Multiton ;
10+
11+
12+ class Multiton
13+ {
14+ /**
15+ *
16+ * 第一个实例
17+ */
18+ const INSTANCE_1 = '1 ' ;
19+
20+ /**
21+ *
22+ * 第二个实例
23+ */
24+ const INSTANCE_2 = '2 ' ;
25+
26+ /**
27+ * 实例数组
28+ *
29+ * @var array
30+ */
31+ private static $ instances = array ();
32+
33+ /**
34+ * 构造函数是私有的,不能从外部进行实例化
35+ *
36+ */
37+ private function __construct ()
38+ {
39+ }
40+
41+ /**
42+ * 通过指定名称返回实例(使用到该实例的时候才会实例化)
43+ *
44+ * @param string $instanceName
45+ *
46+ * @return Multiton
47+ */
48+ public static function getInstance ($ instanceName )
49+ {
50+ if (!array_key_exists ($ instanceName , self ::$ instances )) {
51+ self ::$ instances [$ instanceName ] = new self ();
52+ }
53+
54+ return self ::$ instances [$ instanceName ];
55+ }
56+
57+ /**
58+ * 防止实例从外部被克隆
59+ *
60+ * @return void
61+ */
62+ private function __clone ()
63+ {
64+ }
65+
66+ /**
67+ * 防止实例从外部反序列化
68+ *
69+ * @return void
70+ */
71+ private function __wakeup ()
72+ {
73+ }
74+ }
Original file line number Diff line number Diff line change 1+ # PHP 设计模式 —— 多例模式(Multiton)
2+ ## 1、模式定义
3+ 多例模式和单例模式类似,但可以返回多个实例。比如我们有多个数据库连接,MySQL、SQLite、Postgres,又或者我们有多个日志记录器,分别用于记录调试信息和错误信息,这些都可以使用多例模式实现。
4+
5+ ## 2、UML类图
6+ ![ Multiton-UML.png] ( /static/images/Multiton-UML.png )
Original file line number Diff line number Diff line change 1313* [ 抽象工厂模式(Abstract Factory)] ( https://github.com/IQcoder/DesignerPatterns/blob/master/DesignerPatterns/AbstractFactory/README.md )
1414* [ 建造者模式(Builder)] ( https://github.com/IQcoder/DesignerPatterns/blob/master/DesignerPatterns/Builder/README.md )
1515* [ 工厂方法模式(Factory Method)] ( https://github.com/IQcoder/DesignerPatterns/blob/master/DesignerPatterns/FactoryMethod/README.md )
16+ * [ 多例模式(Multiton)] ( https://github.com/IQcoder/DesignerPatterns/blob/master/DesignerPatterns/Multiton/README.md )
1617
1718#### 结构型
1819* [ 依赖注入模式(Dependency Injection)] ( https://github.com/IQcoder/DesignerPatterns/blob/master/DesignerPatterns/DependencyInjection/README.md )
You can’t perform that action at this time.
0 commit comments