`
lvwenwen
  • 浏览: 932493 次
  • 性别: Icon_minigender_1
  • 来自: 魔都
社区版块
存档分类
最新评论

适配器模式 Adapter Pattern

阅读更多

文章链接:http://blog.csdn.net/surprisesdu/article/details/606148

适配器模式(Adapter Pattern)[GOF95]把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
问题:
我们已经在应用程序中设计了接口sampleOperation1, 然后我们又为了加快开发而购买了可以很好的完成此功能的第三方库, 然而第三方库的接口sampleOperation2和我们设计的接口sampleOperation1不同, 为了使这些接口不兼容的类可以一起工作, 我们应用Adapter模式将第三方库的接口sampleOperation2转换为我们的接口sampleOperation1 (也是客户希望的接口).
适配器模式有两种形式.

一、             类的适配器模式
类图:

源代码:
package com.designpatterns.adapter;

/**
* 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口
* @author suki
*/
public interface Target {     
     public void sampleOperation1();   
}

/**
* 源角色,相当于第三方库
* @author suki
*/
public class Adaptee {
     public Adaptee() {
     }
   
     public void sampleOperation2()
     {
            System.out.println("Adaptee.sampleOperation2()");
     }
}

/**
* 适配器角色
* 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1
* * @author suki
*/
public class Adapter extends Adaptee implements Target {
     public void sampleOperation1() {
            this.sampleOperation2();
     }
}

/**
* 客户端类
* @author suki
*/
public class Client {
       public static void main(String[] args) {
              Adaptee adaptee = new Adaptee();
              Target target = new Adapter();
              target.sampleOperation1();
       }
}
源代码:
package com.designpatterns.adapter;

/**
* 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口
* @author suki
*/
public interface Target {     
     public void sampleOperation1();   
}

/**
* 源角色,相当于第三方库
* @author suki
*/
public class Adaptee {
     public Adaptee() {
     }
   
     public void sampleOperation2()
     {
            System.out.println("Adaptee.sampleOperation2()");
     }
}

/**
* 适配器角色
* 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1
* * @author suki
*/
public class Adapter extends Adaptee implements Target {
     public void sampleOperation1() {
            this.sampleOperation2();
     }
}

/**
* 客户端类
* @author suki
*/
public class Client {
       public static void main(String[] args) {
              Adaptee adaptee = new Adaptee();
              Target target = new Adapter();
              target.sampleOperation1();
       }
}
二、                 对象的适配器模式
在类的适配器模式中Adapter采用继承的方式复用Adaptee的接口,而在对象的适配器模式中Adapter则采用组合的方式实现Adaptee的复用
Interface
Target

+sampleOperation1:void
Adaptee

+sampleOperation2:void
Adapter
-adaptee:Adaptee
+Adapter
+sampleOperation1:void
类图:

源代码:
package com.designpatterns.adapterofobject;

/**
* 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口
* @author suki
*/
public interface Target {
   public void sampleOperation1();
}
   
/**
* 源角色,相当于第三方库
* @author suki
*/
public class Adaptee {
   public Adaptee() {
     }

   public void sampleOperation2() {
           System.out.println("Adaptee.sampleOperation2()");
   }
}

/**
* 适配器角色
* 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1
* @author suki
*/
public class Adapter implements Target {
    private Adaptee adaptee;
   
    public Adapter(Adaptee adaptee)
    {
           super();
           this.adaptee = adaptee;
    }
   
    public void sampleOperation1() {
            adaptee.sampleOperation2();
     }
}

/**
* 客户端类
* @author suki
*/
public class Client {
      public static void main(String[] args) {
           Adaptee adaptee = new Adaptee();
           Target target = new Adapter(adaptee);
           target.sampleOperation1();
      }
}
 

 

分享到:
评论

相关推荐

    设计模式之适配器模式(Adapter Pattern)

    设计模式之适配器模式(Adapter Pattern) 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

    Head First 设计模式 (七) 适配器模式(Adapter pattern) C++实现

    Head First 设计模式 (七) 适配器模式(Adapter pattern) C++实现

    Java24种设计模式,Java24种设计模式,24种设计模式,学会了这24种设计模式,可以打遍天下无敌手,设计模式非常重要

    8、适配器模式ADAPTER PATTERN 9、模板方法模式TEMPLATE METHOD PATTERN 10、建造者模式BUILDER PATTERN 11、桥梁模式BRIDGE PATTERN 12、命令模式COMMAND PATTERN 13、装饰模式DECORATOR PATTERN 14、迭代器模式...

    适配器模式(Adapter Pattern)原理图

    - **Adapter(适配器)**:这是实现目标接口并含有适配者实例的类。适配器的作用是将适配者的接口转换为目标接口。 2. **工作原理**: - 适配器模式通过引入一个中间层即适配器来解决两个不同接口之间的兼容性问题...

    设计模式:结构型-适配器模式

    适配器模式(Adapter Pattern) 是作为两个不兼容接口之间的桥梁, 这种类型的设计模式属于结构型模式。 一些书籍也称适配器模式为缺省适配器模式(Default Adapter Pattern) 。 适配器模式主要分 为三类: 类...

    设计模式 之 “适配器模式[Adapter Pattern]”

    NULL 博文链接:https://lym6520.iteye.com/blog/713094

    适配器模式

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个...

    .NET设计模式(8):适配器模式(AdapterPattern)

    .NET设计模式(8):适配器模式(AdapterPattern)

    java设计模式之适配器模式

    java设计模式之适配器模式,希望对大家有所帮助。

    设计模式_适配器模式.zip

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个...

    设计模式之适配器模式C++实现

    用c++方法实现AdapterPattern模式

    Python设计模式之适配器模式原理与用法详解

    本文实例讲述了Python设计模式之...适配器模式(Adapter Pattern):将一个类的接口转换成为客户希望的另外一个接口.Adapter Pattern使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 应用场景:系统数据和

    C#版 24种设计模式

    适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...

    24种设计模式与6大设计原则

    适配器模式[ADAPTER PATTERN] 模板方法模式[TEMPLATE METHOD PATTERN] 建造者模式[BUILDER PATTERN] 策略模式 代理模式 单例模式 多例模式 工厂方法模式 抽象工厂模式 门面模式 适配器模式 模板方法模式 建造者模式...

    用Java实现23种设计模式

    适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 过滤器模式(Filter、Criteria Pattern) 组合模式(Composite Pattern) 装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式...

    C#设计模式_设计模式_C#_

    适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式...

    Java设计模式,并加上个人理解

    7. 适配器模式 (Adapter Pattern) 8. 模板方法模式 (Template Pattern) 9. 建造者模式 (Builder Pattern) 10. 原型模式 (Prototype Pattern) 11. 组合模式 (Composite Pattern) 12. 装饰者模式 ...

    C#设计模式(23种设计模式)

    适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. ...

    设计模式PPT

     适配器模式(Adapter Pattern)  桥接模式(Bridge Pattern)  组合模式(Composite Pattern)  装饰者模式(Decorator Pattern)  外观模式(Facade Pattern)  享元模式(Flyweight Pattern)  ...

Global site tag (gtag.js) - Google Analytics