php常用设计模式整理

策略模式
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
<?php
/**
* 策略模式
* 定义一系列的算法,把每一个算法封装起来, 并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化
*
*/
/**
* 出行旅游
*
*
*/
interface TravelStrategy{
public function travelAlgorithm();
}
/**
* 具体策略类(ConcreteStrategy)1:乘坐飞机
*/
class AirPlanelStrategy implements TravelStrategy {
public function travelAlgorithm(){
echo "travel by AirPlain", "<BR>\r\n";
}
}
/**
* 具体策略类(ConcreteStrategy)2:乘坐火车
*/
class TrainStrategy implements TravelStrategy {
public function travelAlgorithm(){
echo "travel by Train", "<BR>\r\n";
}
}
/**
* 具体策略类(ConcreteStrategy)3:骑自行车
*/
class BicycleStrategy implements TravelStrategy {
public function travelAlgorithm(){
echo "travel by Bicycle", "<BR>\r\n";
}
}
/**
*
* 环境类(Context):用一个ConcreteStrategy对象来配置。维护一个对Strategy对象的引用。可定义一个接口来让Strategy访问它的数据。
* 算法解决类,以提供客户选择使用何种解决方案:
*/
class PersonContext{
private $_strategy = null;
public function __construct(TravelStrategy $travel){
$this->_strategy = $travel;
}
/**
* 旅行
*/
public function setTravelStrategy(TravelStrategy $travel){
$this->_strategy = $travel;
}
/**
* 旅行
*/
public function travel(){
return $this->_strategy ->travelAlgorithm();
}
}
// 乘坐火车旅行
$person = new PersonContext(new TrainStrategy());
$person->travel();
// 改骑自行车
$person->setTravelStrategy(new BicycleStrategy());
$person->travel();
?>
观察者模式
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
<?php
/**
*参考IBM官网:http://www.ibm.com/developerworks/cn/opensource/os-php-designptrns/
*@author http://www.phpddt.com
*/
//观察者
interface IObserver
{
public function notify();
}
//定义可以被观察的对象接口
interface IObservable
{
public function addObserver($observer);
}
//实现IObservable接口
class MessageSystem Implements IObservable
{
private $_observers = array();
public function addObserver($observer)
{
$this->_observers = $observer;
}
public function doNotify()
{
foreach($this->_observers as $o)
{
$o->notify();
}
}
}
//实现IObserver接口
class User Implements IObserver
{
public function __construct($username)
{
echo "我是新用户{$username}<br/>";
}
//通知观察者方法
public function notify()
{
echo '欢迎新用户';
}
}
//使用
$u = new MessageSystem();
$u->addObserver(new User('小明'));
//$u->addObserver(new User('小红'));
//$u->addObserver(new User('小黑'));
$u->doNotify();
单例模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A
{
protected static $_instance = null;
protected function __construct()
{
//disallow new instance
}
protected function __clone(){
//disallow clone
}
public function getInstance()
{
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
}
工厂模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class DbFactory
{
function static factory($db_class_name)
{
$db_class_name = strtolower($db_class_name);
if (include_once 'Drivers/' . $db_class_name . '.php') {
$classname = 'Driver_' . $db_class_name;
return new $db_class_name;
} else {
throw new Exception ('对应的数据库类没找到');
}
}
}
适配器模式
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
<?php
//目标角色
interface Target {
public function simpleMethod1();
public function simpleMethod2();
}
//源角色
class Adaptee {
public function simpleMethod1(){
echo 'Adapter simpleMethod1';
}
}
//类适配器角色
class Adapter implements Target {
private $adaptee;
function __construct(Adaptee $adaptee) {
$this->adaptee = $adaptee;
}
//委派调用Adaptee的sampleMethod1方法
public function simpleMethod1(){
echo $this->adaptee->simpleMethod1();
}
public function simpleMethod2(){
echo 'Adapter simpleMethod2';
}
}
//客户端
class Client {
public static function main() {
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
$adapter->simpleMethod1();
$adapter->simpleMethod2();
}
}
Client::main();
?>
装饰器模式
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
//被装饰者基类
interface Component
{
public function operation();
}
//装饰者基类
abstract class Decorator implements Component
{
protected $component;
public function __construct(Component $component)
{
$this->component = $component;
}
public function operation()
{
$this->component->operation();
}
}
//具体装饰者类
class ConcreteComponent implements Component
{
public function operation()
{
echo 'do operation'.PHP_EOL;
}
}
//具体装饰类A
class ConcreteDecoratorA extends Decorator {
public function __construct(Component $component) {
parent::__construct($component);
}
public function operation() {
parent::operation();
$this->addedOperationA(); // 新增加的操作
}
public function addedOperationA() {
echo 'Add Operation A '.PHP_EOL;
}
}
//具体装饰类B
class ConcreteDecoratorB extends Decorator {
public function __construct(Component $component) {
parent::__construct($component);
}
public function operation() {
parent::operation();
$this->addedOperationB();
}
public function addedOperationB() {
echo 'Add Operation B '.PHP_EOL;
}
}
class Client {
public static function main() {
/*
do operation
Add Operation A
*/
$decoratorA = new ConcreteDecoratorA(new ConcreteComponent());
$decoratorA->operation();
/*
do operation
Add Operation A
Add Operation B
*/
$decoratorB = new ConcreteDecoratorB($decoratorA);
$decoratorB->operation();
}
}
Client::main();
转自: http://www.phpddt.com/php/design-decoration.html