1 <?php
2
3 /**
4 * 门面模式(Facade)又称外观模式,用于为子系统中的一组接口提供一个一致的界面。
5 * 门面模式定义了一个高层接口,这个接口使得子系统更加容易使用:引入门面角色之后,
6 * 用户只需要直接与门面角色交互,用户与子系统之间的复杂关系由门面角色来实现,从而降低了系统的耦合度。
7 */
8
9
10 interface Os
11 {
12 /**
13 * halt the OS
14 */
15 public function halt();
16 }
17
18 interface Bios
19 {
20 /**
21 * execute the BIOS
22 */
23 public function execute();
24
25 /**
26 * wait for halt
27 */
28 public function waitForKeyPress();
29
30 /**
31 * launches the OS
32 *
33 * @param OsInterface $os
34 */
35 public function launch(OsInterface $os);
36
37 /**
38 * power down BIOS
39 */
40 public function powerDown();
41 }
42
43
44
45
46
47 class Facade
48 {
49 /**
50 * @var OsInterface
51 */
52 protected $os;
53
54 /**
55 * @var BiosInterface
56 */
57 protected $bios;
58
59
60 /**
61 * This is the perfect time to use a dependency injection container
62 * to create an instance of this class
63 *
64 * @param BiosInterface $bios
65 * @param OsInterface $os
66 */
67 public function __construct(BiosInterface $bios, OsInterface $os)
68 {
69 $this->bios = $bios;
70 $this->os = $os;
71 }
72
73 /**
74 * turn on the system
75 */
76 public function turnOn()
77 {
78 $this->bios->execute();
79 $this->bios->waitForKeyPress();
80 $this->bios->launch($this->os);
81 }
82
83 /**
84 * turn off the system
85 */
86 public function turnOff()
87 {
88 $this->os->halt();
89 $this->bios->powerDown();
90 }
91 }