GoF--适配器设计模式

1.概念: 
  适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。 
2.形式 
  a.类的适配器模式 b.对象的适配器模式 c.缺省适配器模式

3. 模式中的角色

  3.1 目标接口(Target):客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。

  3.2 需要适配的类(Adaptee):需要适配的类或适配者类。

  3.3 适配器(Adapter):通过包装一个需要适配的对象,把原接口转换成目标接口。 

4.适配器模式的类图

  4.1 类的适配器模式:采用继承实现

  4.2 对象的适配器模式:采用对象组合方式实现

  4.3 缺省适配器模式:采用抽象类过渡实现

 

 

5 类的适配器设计模式

类适配器,因为 Adapter 类既继承了 Adaptee (被适配类),也实现了 Target 接口,在 Client 类中我们可以根据需要选择并创建任一种符合需求的子类,来实现具体功能。

   Interface : Target

package edu.gof.adapter.class_;

//目标接口,或称为标准接口
public interface Target {

    public void request();
}

  Class : Adaptee

package edu.gof.adapter.class_;

//已存在的、具有特殊功能、但不符合我们既有的标准接口的类 
public class Adaptee {
    
    public void specificRequest(){
        System.out.println("被适配的类具有的  特殊功能...");
    }

}

  Class : ConcreteTarget

package edu.gof.adapter.class_;

//具体目标类,只提供普通功能
public class ConcreteTarget implements Target {

    public void request() {
        System.out.println("普通类 具有  普通功能...");
    }

}

  Class : Adapter

package edu.gof.adapter.class_;

//适配器类,继承了被适配类,同时实现标准接口
public class Adapter extends Adaptee implements Target{

    public void request() {
        super.specificRequest();
    }

}

  Class : AdapterTest

package edu.gof.adapter.class_;

public class AdapterTest {

    public static void main(String[] args) {
        Target clientA = new ConcreteTarget();
        clientA.request();
        
        Target clientB = new Adapter();
        clientB.request();
    }

}

  Console : 

普通类 具有  普通功能...
被适配的类具有的  特殊功能...

6.对象的适配器设计模式

对象适配器模式,可以使得 Adapter 类(适配类)根据传入的 Adaptee 对象达到适配多个不同被适配类的功能。

  Interface :Target

package edu.gof.adapter.object_;

public interface Target {

    public void request();
}

  Class : Adatee

package edu.gof.adapter.object_;

public class Adaptee {

    public void specificRequest(){
        System.out.println("被适配类 具有 特殊功能...");
    }
}

  Class :ConcreteTarget

package edu.gof.adapter.object_;

public class ConcreteTarget implements Target {

    public void request() {
        System.out.println("普通类 具有 普通功能...");
    }

}

  Class : Adapter

package edu.gof.adapter.object_;


public class Adapter implements Target{
//    直接关联被适配类
    private Adaptee adaptee;

    public Adapter() {
        super();
    }
//    可以通过构造函数传入具体需要适配的被适配类对象 
    public Adapter(Adaptee adaptee) {
        super();
        this.adaptee = adaptee;
    }

    public void request() {
//        这里是使用委托的方式完成特殊功能 
        this.adaptee.specificRequest();
    }

}

  Class :AdapterTest

package edu.gof.adapter.object_;

public class AdapterTest {

    public static void main(String[] args){
        Target clientA = new ConcreteTarget();
        clientA.request();
        
        Target clientB = new Adapter(new Adaptee());
        clientB.request();
    }
}

  Console :

普通类 具有 普通功能...
被适配类 具有 特殊功能...

7.缺省适配器设计模式

缺省适配(Default Adapter)模式为一个接口提供缺省实现,这样子类型可以从这个缺省实现进行扩展,而不必从原有接口进行扩展。作为适配器模式的一个特例,缺省是适配模式在JAVA语言中有着特殊的应用。

  Interface :Target

package edu.gof.adapter.abstract_;

public interface Target {

    public void request();
    
    public void response();
    
}

  Class : AbstractTarget

package edu.gof.adapter.abstract_;

public class AbstractTarget implements Target{

    public void request() {
        // TODO Auto-generated method stub
        
    }

    public void response() {
        // TODO Auto-generated method stub
        
    }
}

  Class :Adapter

package edu.gof.adapter.abstract_;

public class Adapter extends AbstractTarget {
    
    public void request(){
        System.out.println("适配器类 只适配 需要的 功能...");
    }
}

  Class : AdapterTest

package edu.gof.adapter.abstract_;

public class AdapterTest {

    public static void main(String[] args){
        Target clientA = new Adapter();
        clientA.request();
        clientA.response();
    }
}

  Console :

适配器类 只适配 需要的 功能...

啦啦啦

啦啦啦

啦啦啦

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值