PHP璁捐妯″紡绗旇锛氫娇鐢≒HP瀹炵幇闂ㄩ潰妯″紡
銆愭剰鍥俱€?/strong> 銆愰棬闈㈡ā寮忕粨鏋勫浘銆?/strong>
銆愰棬闈㈡ā寮忎腑涓昏瑙掕壊銆?/strong>
瀛愮郴缁?subsystem)瑙掕壊锛?br />
瀹炵幇瀛愮郴缁熺殑鍔熻兘 銆愰棬闈㈡ā寮忕殑浼樼偣銆?/strong>
銆愰棬闈㈡ā寮忛€傜敤鍦烘櫙銆?/strong>
銆愰棬闈㈡ā寮忎笌鍏跺畠妯″紡銆?/strong> 銆愰棬闈㈡ā寮廝HP绀轰緥銆?/strong>
杞浇璇蜂互閾炬帴褰㈠紡娉ㄦ槑鍘熷鍑哄鍜屼綔鑰咃紝璋㈢粷涓嶅皧閲嶇増鏉冭€呮妱琚紒 鏍囩锛?a href="http://www.phppan.com/tag/%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f/" rel="tag">璁捐妯″紡,
涓哄瓙绯荤粺涓殑涓€缁勬帴鍙f彁渚涗竴涓竴鑷寸殑鐣岄潰锛孎acade妯″紡瀹氫箟浜嗕竴涓珮灞傛鐨勬帴鍙o紝浣垮緱瀛愮郴缁熸洿鍔犲鏄撲娇鐢ㄣ€怗OF95銆?br />
澶栭儴涓庡瓙绯荤粺鐨勯€氫俊鏄€氳繃涓€涓棬闈?Facade)瀵硅薄杩涜銆?/p>
闂ㄩ潰(Facade)瑙掕壊锛?br />
姝よ鑹插皢琚鎴风璋冪敤
鐭ラ亾鍝簺瀛愮郴缁熻礋璐e鐞嗚姹?br />
灏嗙敤鎴风殑璇锋眰鎸囨淳缁欓€傚綋鐨勫瓙绯荤粺
澶勭悊鐢盕acade瀵硅薄鎸囨淳鐨勪换鍔?br />
娌℃湁Facade鐨勭浉鍏充俊鎭紝鍙互琚鎴风鐩存帴璋冪敤
鍙互鍚屾椂鏈変竴涓垨澶氫釜瀛愮郴缁燂紝姣忎釜瀛愮郴缁熼兘涓嶆槸涓€涓崟鐙殑绫伙紝鑰屼竴涓被鐨勯泦鍚堛€傛瘡涓瓙绯荤粺閮藉彲浠ヨ瀹㈡埛绔洿鎺ヨ皟鐢紝鎴栬€呰闂ㄩ潰瑙掕壊璋冪敤銆傚瓙绯荤粺骞剁煡閬撻棬闈㈡ā寮忕殑瀛樺湪锛屽浜庡瓙绯荤粺鑰岃█锛岄棬闈粎浠呮槸鍙︿竴涓鎴风銆?/p>
1銆佸畠瀵瑰鎴峰睆钄戒簡瀛愮郴缁熺粍浠讹紝鍥犺€屽噺灏戜簡瀹㈡埛澶勭悊鐨勫璞$殑鏁扮洰骞朵娇寰楀瓙绯荤粺浣跨敤璧锋潵鏇村姞鏂逛究
2銆佸疄鐜颁簡瀛愮郴缁熶笌瀹㈡埛涔嬮棿鐨勬澗鑰﹀悎鍏崇郴
3銆佸鏋滃簲鐢ㄩ渶瑕侊紝瀹冨苟涓嶉檺鍒跺畠浠娇鐢ㄥ瓙绯荤粺绫汇€傚洜姝ゅ彲浠ュ湪绯荤粺鏄撶敤鎬т笌鑳界敤鎬т箣闂村姞浠ラ€夋嫨
1銆佷负涓€浜涘鏉傜殑瀛愮郴缁熸彁渚涗竴缁勬帴鍙?br />
2銆佹彁楂樺瓙绯荤粺鐨勭嫭绔嬫€?br />
3銆佸湪灞傛鍖栫粨鏋勪腑锛屽彲浠ヤ娇鐢ㄩ棬闈㈡ā寮忓畾涔夌郴缁熺殑姣忎竴灞傜殑鎺ュ彛
鎶借薄宸ュ巶妯″紡锛欰bstract Factory妯″紡鍙互涓嶧acade妯″紡涓€璧蜂娇鐢ㄤ互鎻愪緵涓€涓帴鍙o紝杩欎竴鎺ュ彛鍙敤鏉ヤ互涓€绉嶅瓙绯荤粺鐙珛鐨勬柟寮忓垱寤哄瓙绯荤粺瀵硅薄銆侫bstract Factory妯″紡涔熷彲浠ヤ唬鏇縁acade妯″紡闅愯棌閭d簺涓庡钩鍙扮浉鍏崇殑绫?br />
璋冨仠鑰呮ā寮忥細Mediator妯″紡涓嶧acade妯″紡鐨勭浉浼间箣澶勬槸锛屽畠鎶借薄浜嗕竴浜涘凡鏈夌被鐨勫姛鑳姐€傜劧鑰岋紝Mediator鐩殑鏄鍚屼簨涔嬮棿鐨勪换鎰忛€氳杩涜鎶借薄锛岄€氬父闆嗕腑涓嶅睘浜庝换浣曞崟涓璞$殑鍔熻兘銆侻ediator鐨勫悓浜嬪璞$煡閬撲腑浠嬭€呭苟涓庡畠閫氫俊锛岃€屼笉鏄洿鎺ヤ笌鍏朵粬鍚岀被瀵硅薄閫氫俊銆傜浉瀵硅€岃█锛孎acade妯″紡浠呭瀛愮郴缁熷璞$殑鎺ュ彛杩涜鎶借薄锛屼粠鑰屼娇瀹冧滑鏇村鏄撲娇鐢紱瀹冨苟瀹氫箟涓嶅姛鑳斤紝瀛愮郴缁熶篃涓嶇煡閬揻acade鐨勫瓨鍦?br />
鍗曚緥妯″紡锛氫竴鑸潵璇达紝浠呴渶瑕佷竴涓狥acade瀵硅薄锛屽洜姝acade瀵硅薄閫氬父灞炰簬Singleton瀵硅薄銆?/p>
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* 闂ㄩ潰妯″紡 2010-06-12 sz
* 銆奐ava涓庢ā寮忋€嬩笂闂ㄩ潰妯″紡绀轰緥鐨凱HP鐗堟湰
* @author phppan.p#gmail.com http://www.phppan.com
* 鍝ュ绀炬垚鍛橈紙http://www.blog-brother.com/锛? * @package design pattern
*/
class Camera {
/**
* 鎵撳紑褰曞儚鏈? */
public function turnOn() {
echo 'Turning on the camera.<br />';
}
/**
* 鍏抽棴褰曞儚鏈? */
public function turnOff() {
echo 'Turning off the camera.<br />';
}
/**
* 杞埌褰曞儚鏈? * @param <type> $degrees
*/
public function rotate($degrees) {
echo 'rotating the camera by ', $degrees, ' degrees.<br />';
}
}
class Light {
/**
* 寮€鐏? */
public function turnOn() {
echo 'Turning on the light.<br />';
}
/**
* 鍏崇伅
*/
public function turnOff() {
echo 'Turning off the light.<br />';
}
/**
* 鎹㈢伅娉? */
public function changeBulb() {
echo 'changing the light-bulb.<br />';
}
}
class Sensor {
/**
* 鍚姩鎰熷簲鍣? */
public function activate() {
echo 'Activating the sensor.<br />';
}
/**
* 鍏抽棴鎰熷簲鍣? */
public function deactivate() {
echo 'Deactivating the sensor.<br />';
}
/**
* 瑙﹀彂鎰熷簲鍣? */
public function trigger() {
echo 'The sensor has been trigged.<br />';
}
}
class Alarm {
/**
* 鍚姩璀︽姤鍣? */
public function activate() {
echo 'Activating the alarm.<br />';
}
/**
* 鍏抽棴璀︽姤鍣? */
public function deactivate() {
echo 'Deactivating the alarm.<br />';
}
/**
* 鎷夊搷璀︽姤鍣? */
public function ring() {
echo 'Ring the alarm.<br />';
}
/**
* 鍋滄帀璀︽姤鍣? */
public function stopRing() {
echo 'Stop the alarm.<br />';
}
}
/**
* 闂ㄩ潰绫? */
class SecurityFacade {
/* 褰曞儚鏈?*/
private $_camera1, $_camera2;
/* 鐏?*/
private $_light1, $_light2, $_light3;
/* 鎰熷簲鍣?*/
private $_sensor;
/* 璀︽姤鍣?*/
private $_alarm;
public function __construct() {
$this->_camera1 = new Camera();
$this->_camera2 = new Camera();
$this->_light1 = new Light();
$this->_light2 = new Light();
$this->_light3 = new Light();
$this->_sensor = new Sensor();
$this->_alarm = new Alarm();
}
public function activate() {
$this->_camera1->turnOn();
$this->_camera2->turnOn();
$this->_light1->turnOn();
$this->_light2->turnOn();
$this->_light3->turnOn();
$this->_sensor->activate();
$this->_alarm->activate();
}
public function deactivate() {
$this->_camera1->turnOff();
$this->_camera2->turnOff();
$this->_light1->turnOff();
$this->_light2->turnOff();
$this->_light3->turnOff();
$this->_sensor->deactivate();
$this->_alarm->deactivate();
}
}
/**
* 瀹㈡埛绔? */
class Client {
private static $_security;
/**
* Main program.
*/
public static function main() {
self::$_security = new SecurityFacade();
self::$_security->activate();
}
}
Client::main();
?>