三种工厂模式

阅读《设计模式之禅》后的总结。

前言

之前的工厂模式有看过,但是没过多久就会忘,主要还是自己平常写代码业务逻辑的时候缺少思考,可能也就会用个单例模式。然后网上很多有关设计模式的介绍,感觉有些还是介绍的不够具体,或者说只介绍到了工厂模式的一部分吧。

工厂方法模式

工厂方法模式的定义:Define an interface for ceating an object ,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses(定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到器子类。)

1
2
3
4
5
6
7
8
  /**
* 产品类接口
*/
interface Product{
public function method1();
public function method2();

}
1
2
3
4
5
6
7
8
9
10
11
12
  /**
* 具体产品类
*/
public class ConcreteProduct1 implements Product{
public function method1(){
//业务逻辑
}
public function method2(){
//业务逻辑
};

}
1
2
3
4
5
6
  /**
* 工厂类
*/
interface Creator{
public creatorProduct();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /**
* 具体工厂类
*/
public class ConcreteCreator implements Creator{

public function creatorProduct($product){
try{
$productObj = new $product;
}catch(Exception $e){
//异常处理
}
return $productObj;
}
}
1
2
3
4
5
6
7
8
public class Client{
public function main(){
//用工厂类制造product1
$fc = new ConcreteCreator();
$cp1 = $fc->creatorProduct("ConcreteProduct1");
$cp1->method1();
}
}

简单工厂模式

简单工厂模式又称静态工厂模式

就是把上面的方法ConcreteCreator()改为静态就可以了。
优点很明显,就是不用实例化工厂类直接调用工厂方法就可以了。

抽象工厂模式

抽象工厂模式

优点

  • 封装性

  • 产品族内的约束为非公开状态

缺点

  • 产品族的扩展非常困难
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* 抽象工厂模式 2010-05-28 sz
* @author phppan.p#gmail.com
* http://www.phppan.com 哥学社成员(http://www.blog-brother.com/)
* @package design pattern
*/

/**
* 抽象工厂
*/
interface AbstractFactory {
/**
* 创建等级结构为A的产品的工厂方法
*/
public function createProductA();

/**
* 创建等级结构为B的产品的工厂方法
*/
public function createProductB();

}

/**
* 具体工厂1
*/
class ConcreteFactory1 implements AbstractFactory{

public function createProductA() {
return new ProductA1();
}

public function createProductB() {
return new ProductB1();
}
}


/**
* 具体工厂2
*/
class ConcreteFactory2 implements AbstractFactory{

public function createProductA() {
return new ProductA2();
}

public function createProductB() {
return new ProductB2();
}
}

/**
* 抽象产品A
*/
interface AbstractProductA {

/**
* 取得产品名
*/
public function getName();
}

/**
* 抽象产品B
*/
interface AbstractProductB {

/**
* 取得产品名
*/
public function getName();
}

/**
* 具体产品A1
*/
class ProductA1 implements AbstractProductA {
private $_name;

public function __construct() {
$this->_name = 'product A1';
}

public function getName() {
return $this->_name;
}
}


/**
* 具体产品A2
*/
class ProductA2 implements AbstractProductA {
private $_name;

public function __construct() {
$this->_name = 'product A2';
}

public function getName() {
return $this->_name;
}
}


/**
* 具体产品B1
*/
class ProductB1 implements AbstractProductB {
private $_name;

public function __construct() {
$this->_name = 'product B1';
}

public function getName() {
return $this->_name;
}
}

/**
* 具体产品B2
*/
class ProductB2 implements AbstractProductB {
private $_name;

public function __construct() {
$this->_name = 'product B2';
}

public function getName() {
return $this->_name;
}
}


/**
* 客户端
*/
class Client {

/**
* Main program.
*/
public static function main() {
self::run(new ConcreteFactory1());
self::run(new ConcreteFactory2());
}

/**
* 调用工厂实例生成产品,输出产品名
* @param $factory AbstractFactory 工厂实例
*/
public static function run(AbstractFactory $factory) {
$productA = $factory->createProductA();
$productB = $factory->createProductB();
echo $productA->getName(), '<br />';
echo $productB->getName(), '<br />';
}

}

Client::main();
?>

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×