设计模式——适配器模式
2016-07-26 16:46
246 查看
适配器模式有两种:
类的适配器:
对象的适配器:
使用的是关联的关系。
类的适配器:
//需要被适配的类 public class Adaptee{ public void printAdaptee(){ System.ou.println("适配器的类"); } } //目标接口 public interface Target{ void printtarget(); } //适配器 public class Adapter extends Adaptee implements Target{ public void printtarget(){ super.printAdaptee(); } } //测试 public sattic void main(String[] args){ Target target=new Adapter(); target.printtarget(); } /* 这样通过继承和实现接口的形式就将原来的Adaptee里的printAdaptee(),方法实现到 Target里 */
对象的适配器:
使用的是关联的关系。
//需要被适配的类 public class Adaptee{ public void printAdaptee(){ System.ou.println("适配器的类"); } } //目标接口 public interface Target{ void printtarget(); } //适配器 public class Adapter implements Target{ private Adaptee adaptee; public Adapter(Adaptee adaptee){ this.adaptee=adaptee; } public void printtarget(){ adaptee.printAdaptee(); } } //测试 public sattic void main(String[] args){ Target target=new Adapter(new Adaptee()); target.printtarget(); } /* 这样通过继承和关联的形式就将原来的Adaptee里的printAdaptee(),方法实现到 Target里 */
相关文章推荐
- PropertyChangeListener简单理解
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 七、设计模式——装饰模式
- 设计模式总结
- 设计模式之创建型模式
- 浅谈设计模式的学习
- 设计模式---状态模式在web前端中的应用
- Ruby设计模式编程之适配器模式实战攻略
- 实例讲解Ruby使用设计模式中的装饰器模式的方法
- 设计模式中的模板方法模式在Ruby中的应用实例两则
- Ruby设计模式编程中对外观模式的应用实例分析
- 实例解析Ruby设计模式编程中Strategy策略模式的使用
- Ruby中使用设计模式中的简单工厂模式和工厂方法模式
- Ruby使用设计模式中的代理模式与装饰模式的代码实例
- 详解组合模式的结构及其在Ruby设计模式编程中的运用
- C# 设计模式系列教程-建造者模式
- C#编程中使用设计模式中的原型模式的实例讲解
- 使用设计模式中的工厂方法模式进行C#编程的示例讲解
- 实例解析C#设计模式编程中简单工厂模式的使用