适配器设计模式(Adapter Pattern)
2017-02-12 21:40
387 查看
GOF设计模式
Adapter Pattern
适配器设计模式
适配器模式比喻
• 在朋友聚会上碰到了一个美女Sarah,从香港来的,可我不会说粤语,她不会说普通话,只好求助于我的朋友kent了,他作为我和Sarah之间的Adapter,让我和Sarah可以相互交谈了(也不知道他会不会耍我)
• 适配器(变压器)模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。
1.类的适配器
public interface Telephone {
public void tel();
}
2.对象的适配器
public interface Telephone2 {
public void tel2();
}
public class Socket2 {
public static void charge2(){
System.out.println("我能充电!");
}
}
Adapter Pattern
适配器设计模式
适配器模式比喻
• 在朋友聚会上碰到了一个美女Sarah,从香港来的,可我不会说粤语,她不会说普通话,只好求助于我的朋友kent了,他作为我和Sarah之间的Adapter,让我和Sarah可以相互交谈了(也不知道他会不会耍我)
• 适配器(变压器)模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。
1.类的适配器
public interface Telephone {
public void tel();
}
public class Socket { public static void charge(){ System.out.println("我能充电!"); } }
public class MyAdapter extends Socket implements Telephone { @Override public void tel() { super.charge(); } }
public class Test { public static void main(String[] args) { MyAdapter a=new MyAdapter(); a.tel(); } }
2.对象的适配器
public interface Telephone2 {
public void tel2();
}
public class Socket2 {
public static void charge2(){
System.out.println("我能充电!");
}
}
public class MyAdapter2 implements Telephone2{ private Socket2 socket2=new Socket2(); @Override public void tel2() { socket2.charge2(); } }
public class Test2 { public static void main(String[] args) { MyAdapter2 a=new MyAdapter2(); a.tel2(); } }
相关文章推荐
- 设计模式(二)-适配器模式(Adapter Pattern)——加个“适配器”以便于复用
- 设计模式-----Adapter(适配器)模式
- [UML][STUDY]设计模式之Adapter(适配器)
- C#设计模式(10)-Adapter Pattern
- 【设计模式】之实例化适配器(Adapter)模式
- 设计模式笔记--结构型模式之一适配器 adapter
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
- 解读设计模式----适配器模式(Adapter Pattern),麻雀飞上枝头真能成凤凰?
- 设计模式学习笔记十一:适配器模式(Adapter Pattern)
- java设计模式学习5--Adapter Pattern[原创]
- 设计模式之Adapter模式(适配器)
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
- 设计模式 - Adapter Pattern(适配器模式)
- 设计模式之Adapter(适配器)
- JAVA设计模式系列之Adapter(适配器)-Java基础-Java-编程开发
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
- .NET设计模式-适配器模式(Adapter Pattern)
- 设计模式之Adapter(适配器)
- Java设计模式:Adapter(适配器)
- 设计模式学习之---Adapter(适配器)模式