62 lines
1.4 KiB
Java
62 lines
1.4 KiB
Java
|
## 外观(Facade)
|
|||
|
|
|||
|
### Intent
|
|||
|
|
|||
|
提供了一个统一的接口,用来访问子系统中的一群接口,从而让子系统更容易使用。
|
|||
|
|
|||
|
### Class Diagram
|
|||
|
|
|||
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/f9978fa6-9f49-4a0f-8540-02d269ac448f.png"/> </div><br>
|
|||
|
|
|||
|
### Implementation
|
|||
|
|
|||
|
观看电影需要操作很多电器,使用外观模式实现一键看电影功能。
|
|||
|
|
|||
|
```java
|
|||
|
public class SubSystem {
|
|||
|
public void turnOnTV() {
|
|||
|
System.out.println("turnOnTV()");
|
|||
|
}
|
|||
|
|
|||
|
public void setCD(String cd) {
|
|||
|
System.out.println("setCD( " + cd + " )");
|
|||
|
}
|
|||
|
|
|||
|
public void startWatching(){
|
|||
|
System.out.println("startWatching()");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public class Facade {
|
|||
|
private SubSystem subSystem = new SubSystem();
|
|||
|
|
|||
|
public void watchMovie() {
|
|||
|
subSystem.turnOnTV();
|
|||
|
subSystem.setCD("a movie");
|
|||
|
subSystem.startWatching();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
```java
|
|||
|
public class Client {
|
|||
|
public static void main(String[] args) {
|
|||
|
Facade facade = new Facade();
|
|||
|
facade.watchMovie();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
### 设计原则
|
|||
|
|
|||
|
最少知识原则:只和你的密友谈话。也就是说客户对象所需要交互的对象应当尽可能少。
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>
|