-
Notifications
You must be signed in to change notification settings - Fork 27
/
4、状态模式.php
110 lines (106 loc) · 2.26 KB
/
4、状态模式.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
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
<?php
/**
* 设计模式之状态模式
* 使用场景 大量和对象状态相关的条件语句
* 小黄牛
*/
header("Content-type: text/html; charset=utf-8");
/**
*
* 派生出相关的接口状态
*/
interface IState{
function WriteCode($Work);
}
/**
* 春季
*/
class AState implements IState{
function WriteCode($Work){
if($Work->hour == '春'){
echo '春季,要下雨啦!<br/>';
}else{
# 调用回调方法,去访问夏季方法
$Work->SetState(new BState());
$Work->WriteCode();
}
}
}
/**
* 夏季
*/
class BState implements IState{
function WriteCode($Work){
if($Work->hour == '夏'){
echo '夏季,好热额!<br/>';
}else{
# 调用回调方法,去访问秋季方法
$Work->SetState(new CState());
$Work->WriteCode();
}
}
}
/**
* 秋季
*/
class CState implements IState{
function WriteCode($Work){
if($Work->hour == '秋'){
echo '秋季,恋爱的季节!<br/>';
}else{
# 调用回调方法,去访问冬季方法
$Work->SetState(new DState());
$Work->WriteCode();
}
}
}
/**
* 冬季
*/
class DState implements IState{
function WriteCode($Work){
if($Work->hour == '冬'){
echo '冬季,浪漫的季节!<br/>';
}else{
echo '你不是地球人!<br/>';
}
}
}
/**
* 处理季节筛选
*/
class Work{
public $hour; // 季节成员
private $current; // 季节对象实例
public $isDone;
/**
* 默认选择第一个筛选项
*/
public function __construct(){
$this->current = new AState();
}
/**
* 当筛选不正确时,调用该方法继续向下筛选
*/
public function SetState($S){
$this->current = $S;
}
/**
* 调用这个方法,使用筛选
*/
public function WriteCode(){
$this->current->WriteCode($this);
}
}
# 实例DEMO
$obj = new Work();
$obj->hour = '春';
$obj->WriteCode();
$obj->hour = '夏';
$obj->WriteCode();
$obj->hour = '秋';
$obj->WriteCode();
$obj->hour = '冬';
$obj->WriteCode();
$obj->hour = '小黄牛';
$obj->WriteCode();