[模式之集思广义] Adapter Pattern

来源:互联网 发布:淘宝怎么设置价格区间 编辑:程序博客网 时间:2024/06/03 00:04

1. Concept: 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。(GoF)

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

2.

http://blog.csdn.net/surprisesdu/article/details/606148

一、            类的适配器模式

 

 

/**

 * 客户端类

 * @author suki

 */

public class Client {

       public static void main(String[] args) {

              Adaptee adaptee = new Adaptee();

              Target target = new Adapter();

              target.sampleOperation1();

       }

}

 

 

二、                对象的适配器模式

/**

 * 客户端类

 * @author suki

 */

public class Client {

      public static void main(String[] args) {

           Adaptee adaptee = new Adaptee();

           Target target = new Adapter(adaptee);

           target.sampleOperation1();

      }

}

 

3. 优先使用组合 而不是继承 ,所以object adapter要优于class adapter

原创粉丝点击