适配器模式(变压器):Adapter
来源:互联网 发布:淘宝入门培训 编辑:程序博客网 时间:2024/06/10 06:06
定义:将一个类的接口转换成客户希望的另外一个接口。A d a p t e r 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
解决问题:为两个不相关的对象组合到一起使用。
举例:1,美国的电器拿到中国来用,中国生活用电的电压是220V,而美国电器支持的是150V, 所以我们用美国电器需要用个变压器把220V改成150V。这个变压器就是一个适配器模式。
2,充电器,因为我们家庭用电是220v,而手机的电板是4v,所以我们用家庭电器需要用表压器把220v改成4v,这个变压器就是一个适配器。
分类:a,类适配器:造成接口污染
b,对象适配器:运用了对象的组合
类适配器与对象适配器的区别:类适配器是通过创建自身来创建一个适配器,对象适配器是直接使用已有的适配器的实例而转换接口,类适配器是继承所以相对来说是静态的,对象适配器是组合来实现的,相对灵活,可以通过写适配器子类来扩展功能.
我比较好的理解:把两个不相关的接口,通过中间类关联起来。
适用性:
- 你想使用一个已经存在的类,而它的接口不符合你的需求。
- 你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。
- (仅适用于对象A d a p t e r )你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。
用Adapter,在这两种接口之间创建一个混合接口(混血儿).
如何使用?
实现Adapter方式,其实"think in Java"的"类再生"一节中已经提到,有两种方式:组合(composition)和继承(inheritance).
假设我们要打桩,有两种类:方形桩 圆形桩.
public class SquarePeg{
public void insert(String str){
System.out.println("SquarePeg insert():"+str);
}
}
public class RoundPeg{
public void insertIntohole(String msg){
System.out.println("RoundPeg insertIntoHole():"+msg);
}
}
现在有一个应用,需要既打方形桩,又打圆形桩.那么我们需要将这两个没有关系的类综合应用.假设RoundPeg我们没有源代码,或源代码我们不想修改,那么我们使用Adapter来实现这个应用:
public class PegAdapter extends SquarePeg{
private RoundPeg roundPeg;
public PegAdapter(RoundPeg peg)(this.roundPeg=peg;)
public void insert(String str){super.insert(str); roundPeg.insertIntoHole(str);}
}
在上面代码中,RoundPeg属于Adaptee,是被适配者.PegAdapter是Adapter,将Adaptee(被适配者RoundPeg)和Target(目标SquarePeg)进行适配.实际上这是将组合方法(composition)和继承(inheritance)方法综合运用.
PegAdapter首先继承SquarePeg,然后使用new的组合生成对象方式,生成RoundPeg的对象roundPeg,再重载父类insert()方法。从这里,你也了解使用new生成对象和使用extends继承生成对象的不同,前者无需对原来的类修改,甚至无需要知道其内部结构和源代码.
如果你有些Java使用的经验,已经发现,这种模式经常使用。
进一步使用
上面的PegAdapter是继承了SquarePeg,如果我们需要两边继承,即继承SquarePeg 又继承RoundPeg,因为Java中不允许多继承,但是我们可以实现(implements)两个接口(interface)
public interface IRoundPeg{
public void insertIntoHole(String msg);
}
public interface ISquarePeg{
public void insert(String str);
}
下面是新的RoundPeg 和SquarePeg, 除了实现接口这一区别,和上面的没什么区别。
public class SquarePeg implements ISquarePeg{
public void insert(String str){
System.out.println("SquarePeg insert():"+str);
}
}
public class RoundPeg implements IRoundPeg{
public void insertIntohole(String msg){
System.out.println("RoundPeg insertIntoHole():"+msg);
}
}
下面是新的PegAdapter,叫做two-way adapter:
public class PegAdapter implements IRoundPeg,ISquarePeg{
private RoundPeg roundPeg;
private SquarePeg squarePeg;
// 构造方法
public PegAdapter(RoundPeg peg){this.roundPeg=peg;}
// 构造方法
public PegAdapter(SquarePeg peg)(this.squarePeg=peg;)
public void insert(String str){ roundPeg.insertIntoHole(str);}
}
- 适配器模式(变压器):Adapter
- Adapter模式 适配器模式
- 适配器模式(Adapter)
- 适配器模式(Adapter)
- 适配器(Adapter)模式
- 适配器模式(Adapter Pattern)
- ADAPTER(适配器)模式
- 适配器模式(adapter)
- Adapter适配器模式
- 适配器(Adapter)模式
- 适配器模式(Adapter Pattern)
- Adapter(适配器)模式
- 适配器模式(adapter)
- 适配器模式(Adapter)
- 适配器模式(Adapter)
- 适配器模式(Adapter)
- 适配器模式 Adapter Pattern
- Adapter 适配器模式
- asp中的数据缓存
- PHPWind数据表说明
- ext
- ext
- (七)清理堆栈
- 适配器模式(变压器):Adapter
- fedora8下的arm交叉编译环境建立完美详介
- C++标准库简介
- C++标准库和C++标准模版库
- 韩寒等50名作家3.15联袂声讨百度侵权
- tcpdump--no suitable device found
- WPF实现简单的反射效果
- (八)进程和线程(Processes and Threads)
- 策略模式:Strategy