java设计模式(结构型)之适配器模式

第0章:简介

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

适配器模式本质:转换匹配,复用功能

参考:研磨设计模式(书籍),大话设计模式(书籍)

模式图:

待补充

第1章:实践

第0节:单向适配器模式

(1)旧功能接口(OldIntf.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 旧功能接口
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-11  下午10:52
 */
public interface OldIntf {

    public void oldMethod();
}

(2)旧功能(OldIntfImpl.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 旧功能
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-11  下午10:53
 */
public class OldIntfImpl implements OldIntf {

    @Override
    public void oldMethod() {
        System.out.println("旧功能方法");
    }
}

(3)新功能接口(NewIntf.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 新功能接口
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-11  下午11:02
 */
public interface NewIntf {

    public void newMethod();
}

(4)新功能(NewIntfImpl.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 新功能
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-12  上午12:38
 */
public class NewIntfImpl implements NewIntf {
    @Override
    public void newMethod() {
        System.out.println("新功能方法");
    }
}

(5)适配器(Adapter.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 适配器
 *
 * 适配器模式的定义:将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
 * 适配器模式的本质:转换匹配,复用功能
 *
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-11  下午11:07
 */
public class Adapter implements NewIntf {

    //持有被适配的接口对象,即旧接口对象
    private OldIntf oldIntf;

    /**
     * 构造器,传入被适配的对象
     * @param oldIntf
     */
    public Adapter(OldIntf oldIntf){
       this.oldIntf = oldIntf;
    }

    /**
     * 适配旧功能方法
     */
    @Override
    public void newMethod() {
        oldIntf.oldMethod();
    }
}

(6)客户端测试(Client.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 客户端测试
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-11  下午11:21
 */
public class Client {

    public void test(NewIntf newIntf){
        newIntf.newMethod();
    }

    public static void main(String args[]){

        Client client = new Client();

        //调用新功能方法
        client.test(new NewIntfImpl());

        //适配方式调用旧功能方法
        client.test(new Adapter(new OldIntfImpl()));

    }
}

第1节:双向适配器模式

(1)旧功能接口(OldIntf.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 旧功能接口
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-11  下午10:52
 */
public interface OldIntf {

    public void oldMethod();
}

(2)旧功能(OldIntfImpl.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 旧功能
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-11  下午10:53
 */
public class OldIntfImpl implements OldIntf {

    @Override
    public void oldMethod() {
        System.out.println("旧功能方法");
    }
}

(3)新功能接口(NewIntf.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 新功能接口
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-11  下午11:02
 */
public interface NewIntf {

    public void newMethod();
}

(4)新功能(NewIntfImpl.java)

package com.mcc.core.designPattern.structure.adapter.singleAdapter;

/**
 * 新功能
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-12  上午12:38
 */
public class NewIntfImpl implements NewIntf {
    @Override
    public void newMethod() {
        System.out.println("新功能方法");
    }
}

(5)双向适配器(Adapter.java)

package com.mcc.core.designPattern.structure.adapter.doubleAdapter;

/**
 * 适配器,双向
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-12  上午12:13
 */
public class Adapter implements NewIntf,OldIntf {
    //持有新功能接口
    private NewIntf newIntf;
    //持有旧功能接口
    private OldIntf oldIntf;

    /**
     * 构造器,传入需要被适配的对象,这里包括新旧功能对象
     * @param newIntf
     * @param oldIntf
     */
    public Adapter(NewIntf newIntf,OldIntf oldIntf){
        this.newIntf = newIntf;
        this.oldIntf = oldIntf;
    }

    /**
     * 新功能方法实现,实现旧功能方法调用,可加入其他业务逻辑
     */
    @Override
    public void newMethod() {
        oldIntf.oldMethod();
    }

    /**
     * 旧功能方法实现,实现新功能方法调用,可加入其他业务逻辑
     */
    @Override
    public void oldMethod() {
        newIntf.newMethod();
    }
}

(6)旧客户端测试(OldClient.java)

package com.mcc.core.designPattern.structure.adapter.doubleAdapter;

/**
 * 旧客户端测试
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-12  上午12:20
 */
public class OldClient {

    /**
     * 调用旧接口中的方法,不受变动影响
     * @param oldIntf
     */
    public void test(OldIntf oldIntf){
        oldIntf.oldMethod();
    }

    public static void main(String args[]){
        OldClient oldClient = new OldClient();

        //调用旧功能方法
        oldClient.test(new OldIntfImpl());

        //适配方式调用新功能方法
        oldClient.test(new Adapter(new NewIntfImpl(),new OldIntfImpl()));

    }
}

(7)新客户端测试(NewClient.java)

package com.mcc.core.designPattern.structure.adapter.doubleAdapter;

/**
 * 新客户端测试
 *
 * @author <a href="mailto:417877417@qq.com">menergy</a>
 *         DateTime: 14-3-12  上午12:30
 */
public class NewClient {

    /**
     * 调用新接口中的方法,不受变动影响
     * @param newIntf
     */
    public void test(NewIntf newIntf){
        newIntf.newMethod();
    }

    public static void main(String args[]){
        NewClient newClient = new NewClient();

        //调用新功能方法
        newClient.test(new NewIntfImpl());

        //适配方式调用旧功能方法
        newClient.test(new Adapter(new NewIntfImpl(),new OldIntfImpl()));
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值