-
Notifications
You must be signed in to change notification settings - Fork 1
/
04-状态模式.php
135 lines (120 loc) · 2.41 KB
/
04-状态模式.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
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
<?php
declare(strict_types=1);
/*
* This file is modified from `xiaohuangniu/26`.
*
* @see https://github.com/xiaohuangniu/26
*/
header('Content-type: text/html; charset=utf-8');
/**
* 派生出相关的接口状态
*/
interface IState
{
public function WriteCode($Work);
}
/**
* 春季.
*/
class AState implements IState
{
public function WriteCode($Work)
{
if ($Work->hour == '春') {
echo '春季,要下雨啦!'.PHP_EOL;
} else {
// 调用回调方法,去访问夏季方法
$Work->SetState(new BState());
$Work->WriteCode();
}
}
}
/**
* 夏季.
*/
class BState implements IState
{
public function WriteCode($Work)
{
if ($Work->hour == '夏') {
echo '夏季,好热额!'.PHP_EOL;
} else {
// 调用回调方法,去访问秋季方法
$Work->SetState(new CState());
$Work->WriteCode();
}
}
}
/**
* 秋季.
*/
class CState implements IState
{
public function WriteCode($Work)
{
if ($Work->hour == '秋') {
echo '秋季,恋爱的季节!'.PHP_EOL;
} else {
// 调用回调方法,去访问冬季方法
$Work->SetState(new DState());
$Work->WriteCode();
}
}
}
/**
* 冬季.
*/
class DState implements IState
{
public function WriteCode($Work)
{
if ($Work->hour == '冬') {
echo '冬季,浪漫的季节!'.PHP_EOL;
} else {
echo '你不是地球人!'.PHP_EOL;
}
}
}
/**
* 处理季节筛选.
*/
class Work
{
public $hour; // 季节成员
public $isDone;
private $current; // 季节对象实例
/**
* 默认选择第一个筛选项.
*/
public function __construct()
{
$this->current = new AState();
}
/**
* 当筛选不正确时,调用该方法继续向下筛选.
* @param mixed $S
*/
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();