喜迎
春节

设计模式——外观模式


说明

外观模式是为了解决类与类之家的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类类之间的耦合度,该模式中没有涉及到接口,看下类图:(我们以一个计算机的启动过程为例)

示例

下面看下外观模式的代码实现

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
class CPU{
public function startup(){
echo "cpu startup!" . PHP_EOL;
}

public function shutdown(){
echo "cpu shutdown!" . PHP_EOL;
}
}

class Memory{
public function startup(){
echo "memory startup!" . PHP_EOL;
}

public function shutdown(){
echo "memory shutdown!" . PHP_EOL;
}
}

class Disk{
public function startup(){
echo "disk startup!" . PHP_EOL;
}

public function shutdown(){
echo "disk shutdown!" . PHP_EOL;
}
}

class Computer{
private CPU $cpu;
private Memory $memory;
private Disk $disk;

public function __construct(){
$this->cpu = new CPU();
$this->memory = new Memory();
$this->disk = new Disk();
}

public function startup(){
echo "start the computer!" . PHP_EOL;
$this->cpu->startup();
$this->memory->startup();
$this->disk->startup();
echo "start computer finished!" . PHP_EOL;
}

public function shutdown(){
echo "begin to close th computer!" . PHP_EOL;
$this->cpu->shutdown();
$this->memory->shutdown();
$this->disk->shutdown();
echo "computer closed!" . PHP_EOL;
}
}


//调用
$computer = new Computer();
$computer->startup();
$computer->shutdown();

/**
* output:
*
start the computer!
cpu startup!
memory startup!
disk startup!
start computer finished!
begin to close th computer!
cpu shutdown!
memory shutdown!
disk shutdown!
computer closed!

class CPU{
public function startup(){
echo "cpu startup!" . PHP_EOL;
}

public function shutdown(){
echo "cpu shutdown!" . PHP_EOL;
}
}

class Memory{
public function startup(){
echo "memory startup!" . PHP_EOL;
}

public function shutdown(){
echo "memory shutdown!" . PHP_EOL;
}
}

class Disk{
public function startup(){
echo "disk startup!" . PHP_EOL;
}

public function shutdown(){
echo "disk shutdown!" . PHP_EOL;
}
}

class Computer{
private CPU $cpu;
private Memory $memory;
private Disk $disk;

public function __construct(){
$this->cpu = new CPU();
$this->memory = new Memory();
$this->disk = new Disk();
}

public function startup(){
echo "start the computer!" . PHP_EOL;
$this->cpu->startup();
$this->memory->startup();
$this->disk->startup();
echo "start computer finished!" . PHP_EOL;
}

public function shutdown(){
echo "begin to close th computer!" . PHP_EOL;
$this->cpu->shutdown();
$this->memory->shutdown();
$this->disk->shutdown();
echo "computer closed!" . PHP_EOL;
}
}


//调用
$computer = new Computer();
$computer->startup();
$computer->shutdown();

/**
* output:
*
start the computer!
cpu startup!
memory startup!
disk startup!
start computer finished!
begin to close th computer!
cpu shutdown!
memory shutdown!
disk shutdown!
computer closed!

*/

总结

如果我们没有Computer类,那么,CPU、Memory、Disk他们之间将会相互持有实例,产生关系,这样会造成严重的依赖,修改一个类,可能会带来其他类的修改,这不是我们想要看到的,有了Computer类,他们之间的关系被放在了Computer类里,这样就起到了解耦的作用,这,就是外观模式!


文章作者: Crazy Boy
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Crazy Boy !
评 论
 上一篇
设计模式——备忘录模式
设计模式——备忘录模式
说明示例下面看下备忘录模式的代码实现 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
2022-05-19
下一篇 
设计模式——工厂方法模式
设计模式——工厂方法模式
说明 创建一个工厂接口和创建多个工厂实现类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码。 示例下面看下工厂方法模式的代码实现 1234567891011121314151617181920212223242
2022-05-19
  目录