设计模式学习之适配器与外观模式
来源:互联网 发布:java的oa项目 编辑:程序博客网 时间:2024/05/16 19:22
定义:适配器模式将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。
适配器(Adapter)分为“对象(Object)”适配器和“类(Class)”适配器两种。
在类适配器中,适配器继承了目标(Target)和被适配者(Adaptee);而对象适配器中,适配器利用组合的方式将请求传送给被适配者。
实例:
让List类型支持枚举类型
public class ListEnumeration implements Enumeration {
private List list;
public ListEnumeration(List al) {
this.list = al;
}
public boolean hasMoreElements() {
return list.iterator().hasNext();
}
public Object nextElement() {
return list.iterator().next();
}
}
外观模式提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。
结构图如下:
适用性:
1 .为一个复杂子系统提供一个简单接口。
2 .提高子系统的独立性。
3 .在层次化结构中,可以使用 Facade 模式定义系统中每一层的入口。
实例:
// 电源
public class Power {
public void connect() {
System.out.println("The power is connected.");
}
public void disconnect() {
System.out.println("The power is disconnected.");
}
}
// 主板
public class MainBoard {
public void on() {
System.out.println("The mainboard is on.");
}
public void off() {
System.out.println("The mainboard is off.");
}
}
// 硬盘
public class HardDisk {
public void run() {
System.out.println("The harddisk is running.");
}
public void stop() {
System.out.println("The harddisk is stopped.");
}
}
// 操作系统
public class OperationSystem {
public void startup() {
System.out.println("The opertion system is startup.");
}
public void shutdown() {
System.out.println("The operation system is shutdown.");
}
}
// 计算机外观
public class Computer {
private Power power;
private MainBoard board;
private HardDisk disk;
private OperationSystem system;
public Computer(Power power, MainBoard board, HardDisk disk, OperationSystem system) {
this.power = power;
this.board = board;
this.disk = disk;
this.system = system;
}
public void startup() {
this.power.connect();
this.board.on();
this.disk.run();
this.system.startup();
}
public void shutdown() {
this.system.shutdown();
this.disk.stop();
this.board.off();
this.power.disconnect();
}
}
public class Power {
public void connect() {
System.out.println("The power is connected.");
}
public void disconnect() {
System.out.println("The power is disconnected.");
}
}
// 主板
public class MainBoard {
public void on() {
System.out.println("The mainboard is on.");
}
public void off() {
System.out.println("The mainboard is off.");
}
}
// 硬盘
public class HardDisk {
public void run() {
System.out.println("The harddisk is running.");
}
public void stop() {
System.out.println("The harddisk is stopped.");
}
}
// 操作系统
public class OperationSystem {
public void startup() {
System.out.println("The opertion system is startup.");
}
public void shutdown() {
System.out.println("The operation system is shutdown.");
}
}
// 计算机外观
public class Computer {
private Power power;
private MainBoard board;
private HardDisk disk;
private OperationSystem system;
public Computer(Power power, MainBoard board, HardDisk disk, OperationSystem system) {
this.power = power;
this.board = board;
this.disk = disk;
this.system = system;
}
public void startup() {
this.power.connect();
this.board.on();
this.disk.run();
this.system.startup();
}
public void shutdown() {
this.system.shutdown();
this.disk.stop();
this.board.off();
this.power.disconnect();
}
}
0 0
- 设计模式学习之适配器与外观模式
- 设计模式之适配器与外观模式
- 设计模式之适配器模式与外观模式
- Head First 设计模式之适配器模式与外观模式
- 设计模式之适配器模式与外观模式
- 设计模式--适配器模式与外观模式
- 设计模式-适配器模式与外观模式
- 设计模式之--适配器模式、外观模式
- 设计模式之适配器模式&外观模式
- 懒羊学习《深入浅出设计模式》C#代码手札之适配器模式与外观模式
- 设计模式之适配器、外观模式
- 设计模式学习(六) ------- 适配器与外观
- php 设计模式之 代理模式,适配器模式,外观模式
- 深入浅出设计模式之命令模式、适配器模式、外观模式
- 设计模式学习笔记十四:适配器模式、桥接模式与外观模式
- 设计模式学习笔记十四:适配器模式、桥接模式与外观模式
- 适配器与外观模式
- [学习笔记]设计模式[6]-{适配器模式&外观模式}
- Surround Threes (凸包模版)
- UVA 10895 - Matrix Transpose(STL)
- VisualAge for Java开发Servlets
- 第三章 链表
- leetcode Insertion Sort List(*)
- 设计模式学习之适配器与外观模式
- DRM进入HTML 5标准 两害相权取其轻?
- 入职一月感受
- Android特效 五种Toast详解
- Square(DFS)
- sgu 414 Orthogonal Circles 圆的正交
- HDU 2814 Interesting Fibonacci 循环节
- CentOs使用yum方式安装wine 并安装Source Insight
- poj3211 Washing Clothes