设计模式之适配器模式

来源:互联网 发布:房产中介软件 亿房通 编辑:程序博客网 时间:2024/06/05 14:21
写作缘由:对于一个有逼格的程序猿,23种设计模式应该不在话下,为了挤身逼格程序猿之列,决定系统的学习设计模式

        关于设计模式(主要是思想方面)

               001.定义:

                               是一套反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结

               002.目的:

                               为了可重用代码、让代码更容易被他人理解、保证代码可靠性

        设计模式之适配器模式:

               定义:


(对象适配器:采用组合的方式的适配器)

目标接口(具体的类或者抽象的类):

/** * 目标接口:三项插座接口 */public interface ThreePlugIf {    //使用三项电流供电    void powerWithThree();}

原本接口:

/** * 需要适配的类:国标2项插座 */public class GBTwoPlug {    private static final String TAG = "GBTwoPlug";    //使用2项电流供电    public void powerWithTwo(){        Log.i(TAG, "powerWithTwo: "+"使用2项电流供电");    }

适配器:

/** * 适配器:将原本的转化为Target */public class TwoPlugAdapter implements ThreePlugIf {    private static final String TAG = "TwoPlugAdapter";    //两项插座    private GBTwoPlug plug;    public TwoPlugAdapter(GBTwoPlug plug) {        this.plug = plug;    }    @Override    public void powerWithThree() {        plug.powerWithTwo();        Log.i(TAG, "powerWithThree: "+"通过转化");    }}

客户端:

/** * 客户端:笔记本类 */public class NoteBook {    //期望的三项插座接口    private ThreePlugIf plug;    public NoteBook(ThreePlugIf plug){        this.plug = plug;    }    //使用插座充电    public void charge(){        plug.powerWithThree();    }}

测试:

//插座适配器private void test3() {    //2项插座    GBTwoPlug twoPlug = new GBTwoPlug();    //2项转化为3项:适配笔记本电脑    ThreePlugIf plug = new TwoPlugAdapter(twoPlug);    NoteBook noteBook = new NoteBook(plug);    noteBook.charge();}

打印:

/com.test.okamiy.factorymode I/TwoPlugAdapter: powerWithThree: 通过转化
/com.test.okamiy.factorymode I/GBTwoPlug: powerWithTwo: 使用2项电流供电


         插座适配器总结:

                 TwoPlugAdapter适配器实现了ThreePlugIf目标接口,它组合适配者类GBTwoPlug作为一个对象组合到适配器类中,以修改目标接口包装被适配者

         该适配器就是一种对象适配器,它有一个特点:

                  被适配者的任何子类都可以搭配适配器使用


(类适配器:采用继承方式)

目标接口(具体的类或者抽象的类):

/** * 目标接口:三项插座接口 */public interface ThreePlugIf {    //使用三项电流供电    void powerWithThree();}

最终需要的接口:

/** * 需要适配的类:国标2项插座 */public class GBTwoPlug {    private static final String TAG = "GBTwoPlug";    //使用2项电流供电    public void powerWithTwo(){        Log.i(TAG, "powerWithTwo: "+"使用2项电流供电");    }}

客户端:

/** * 客户端:笔记本类 */public class NoteBook {    //期望的三项插座接口    private ThreePlugIf plug;    public NoteBook(ThreePlugIf plug){        this.plug = plug;    }    //使用插座充电    public void charge(){        plug.powerWithThree();    }}

适配器:

/** * 类适配器:继承方式 */public class TwoPlugAdapterExtend extends GBTwoPlug implements ThreePlugIf {    private static final String TAG = "TwoPlugAdapterExtend";    //实现了三项接口    @Override    public void powerWithThree() {        Log.i(TAG, "powerWithThree: " + "借助继承适配器");        this.powerWithTwo();//通过继承得到两项接口    }}

测试:

    //插座适配器    private void test3() {//        //2项插座//        GBTwoPlug twoPlug = new GBTwoPlug();//        //2项转化为3项:适配笔记本电脑//        ThreePlugIf plug = new TwoPlugAdapter(twoPlug);////        NoteBook noteBook = new NoteBook(plug);//        noteBook.charge();        ThreePlugIf three = new TwoPlugAdapterExtend();        NoteBook noteBook = new NoteBook(three);        noteBook.charge();    }

打印:

/com.test.okamiy.factorymode I/TwoPlugAdapterExtend: powerWithThree: 借助继承适配器
/com.test.okamiy.factorymode I/GBTwoPlug: powerWithTwo: 使用2项电流供电


                 继承适配器总结:

                        通过继承的方式实现适配称之为类适配器

                        类适配器优点:

                               单一的为某个类(TwoPlugAdapterExtend)提供服务实现适配


还有模式的变体:

       只要是把不兼容的转化为兼容的、匹配的,就是适配器

       分析:

              我们插入抽象类abstract Adapter实现目标接口Target,具体的适配器再继承abstract Adapter抽象适配器,具体的两个类Adapter Impl1和Adapter Impl2实现适配者接口Adaptee,这样适配器Adapter就可以适配适配者Adaptee的任何子类




               Last:本篇参照慕课网视频学习,有不懂的可以去慕课网看视频学习!

           

                       欢迎探讨学习,真心希望大家推荐好文共同成长!
原创粉丝点击