外观模式
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();
}
}
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();
}
}
相关文章推荐
- 设计模式袖珍版 连续转载之 - Facade(外观)
- 包装外观(Wrapper Facade):用于在类中封装函数的结构型模式
- 设计模式之Facade(外观)
- C# 设计模式--外观模式(Facade)
- Java设计模式之外观模式研究
- 设计模式之Facade外观模式
- 设计模式学习笔记(十一)——Facade外观模式
- 设计模式学习笔记(十一)——Facade外观模式
- 设计模式(5)-外观模式(Facade)
- 设计模式(7)-外观模式(Facade)
- Dot Net设计模式—外观模式
- .NET设计模式(12):外观模式(Façade Pattern)
- Java有很多漂亮的外观模式和皮肤
- Dot Net设计模式—适配器、桥接与外观三模式之间的关系
- 会话外观模式与值对象
- 设计模式之Facade(外观 总管 Manager)
- 设计模式之外观(Facade)---对象结构型模式
- Facade外观(结构型模式)
- 外观模式(Facade Pattern)
- 《设计模式精解》学习笔记(四)——Facade(外观)模式