您的位置:首页 > 其它

外观模式

2015-03-03 16:27 447 查看
一、什么是外观模式

Facade模式也叫外观模式,是由GoF提出的23种设计模式中的一种。Facade模式为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。这个一致的简单的界面被称作facade。

二、外观模式的角色和职责

Facade
为调用方定义简单的调用接口。
Clients
调用者。通过Facade接口调用提供某功能的内部类群。
Packages
功能提供者。指提供功能的类群(模块或子系统)。

SystemA.java
/*
* A子系统
*/
public class SystemA {
/*
* A子系统实现功能
*/
public void doSomething() {
System.out.println("实现A子系统功能");
}
}
SystemB.java
/*
* B子系统
*/
public class SystemB {

/*
* B子系统实现功能
*/
public void doSomething() {
System.out.println("实现B子系统功能");
}
}
SystemC.java
/*
* C子系统
*/
public class SystemC {

/*
* C子系统实现功能
*/
public void doSomething() {
System.out.println("实现C子系统功能");
}
}

Facade.java
public class Facade {
private SystemA systemA;
private SystemB systemB;
private SystemC systemC;

public Facade() {
systemA = new SystemA();
systemB = new SystemB();
systemC = new SystemC();
}

public void doABC() {
this.systemA.doSomething();
this.systemB.doSomething();
this.systemC.doSomething();
}

public void doAB() {
this.systemA.doSomething();
this.systemB.doSomething();
}
}
MainClass.java
public class MainClass {
public static void main(String[] args) {
Facade facade = new Facade();
facade.doABC();
}
}
MainClass2.java
public class MainClass2 {
public static void main(String[] args) {
Facade facade = new Facade();
facade.doAB();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息