1    重要

1.1  类适配器和对象适配器最大的区别

 

2    代码演练

2.1  代码演练1(类适配器模式)

2.2  代码演练2(对象适配模式)

2.3  代码演练3(具体应用场景)

 

 

1    重要
1.1  类适配器和对象适配器最大的区别

类适配器通过继承关系达到适配的目的,而对象适配器通过组合达到适配目的。

 

2    代码演练
2.1  代码演练1(类适配器模式)

需求相关:

a类和b类,想用b类或者b类的子类实现a类的方法

 

设计分析:

适配器类c继承了被适配器类a并且实现了目标类b的接口,通过适配器类c把被适配者a的方法适配给了目标类b;

适配器类c是被适配者a的子类,通过调用父类的方法实现了目标类b

 

UML类图:

 

 

测试类:

package com.geely.design.pattern.structural.adapter.classadapter;

public class Test {
    public static void main(String [] args){
        //打印目标类的方法
        Target target = new ConcreteTarget();
        target.request();


        //打印适配器的方法
        Adapter adapter = new Adapter();
        adapter.request();

    }
}

 

适配器类Adapter:

package com.geely.design.pattern.structural.adapter.classadapter;

public class Adapter extends Adaptee implements Target {
    @Override
    public void request() {
        super.adapteeRequest();
    }
}

 

目标接口Target:

package com.geely.design.pattern.structural.adapter.classadapter;

/**
 * 目标接口
 */
public interface Target {
    void request();
}

 

目标实现类ConCreteTarget:

package com.geely.design.pattern.structural.adapter.classadapter;

/**
 * 目标实现类
 */
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        System.out.println("目标实现类自己的方法,,,");
    }
}

 

被适配者类Adaptee:

package com.geely.design.pattern.structural.adapter.classadapter;

/**
 * 该类是被适配者,想实现目标类的方法
 */
public class Adaptee {
    public void adapteeRequest(){
        System.out.println("被适配器执行它自己的方法。。。");
    }
}

 

打印日志:

"C:\Program Files\Java\jdk1.7.0_79\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=33436:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\design_pattern\target\classes;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\apache\tomcat\tomcat-servlet-api\7.0.64\tomcat-servlet-api-7.0.64.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-webmvc\4.0.3.RELEASE\spring-webmvc-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-beans\4.0.3.RELEASE\spring-beans-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-context\4.0.3.RELEASE\spring-context-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-aop\4.0.3.RELEASE\spring-aop-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-core\4.0.3.RELEASE\spring-core-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-expression\4.0.3.RELEASE\spring-expression-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-web\4.0.3.RELEASE\spring-web-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-oxm\4.0.3.RELEASE\spring-oxm-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-jdbc\4.0.3.RELEASE\spring-jdbc-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-tx\4.0.3.RELEASE\spring-tx-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\spring-test\4.0.3.RELEASE\spring-test-4.0.3.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\aspectj\aspectjweaver\1.7.3\aspectjweaver-1.7.3.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\mybatis\mybatis-spring\1.3.0\mybatis-spring-1.3.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\mybatis\mybatis\3.4.1\mybatis-3.4.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\aspectj\aspectjrt\1.6.11\aspectjrt-1.6.11.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\codehaus\jackson\jackson-mapper-asl\1.9.12\jackson-mapper-asl-1.9.12.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\codehaus\jackson\jackson-core-asl\1.9.12\jackson-core-asl-1.9.12.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-pool\commons-pool\1.5.4\commons-pool-1.5.4.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\ch\qos\logback\logback-classic\1.1.2\logback-classic-1.1.2.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\slf4j\slf4j-api\1.7.6\slf4j-api-1.7.6.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\ch\qos\logback\logback-core\1.1.2\logback-core-1.1.2.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\google\guava\guava\20.0\guava-20.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\junit\junit\4.12\junit-4.12.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\joda-time\joda-time\2.3\joda-time-2.3.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\hashids\hashids\1.0.1\hashids-1.0.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-net\commons-net\3.1\commons-net-3.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-fileupload\commons-fileupload\1.2.2\commons-fileupload-1.2.2.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-io\commons-io\2.0.1\commons-io-2.0.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\github\pagehelper\pagehelper\4.1.0\pagehelper-4.1.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\github\miemiedev\mybatis-paginator\1.2.17\mybatis-paginator-1.2.17.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\github\jsqlparser\jsqlparser\0.9.4\jsqlparser-0.9.4.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-configuration\commons-configuration\1.10\commons-configuration-1.10.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\google\zxing\core\2.1\core-2.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\google\code\gson\gson\2.3.1\gson-2.3.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\redis\clients\jedis\2.6.0\jedis-2.6.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\apache\commons\commons-pool2\2.0\commons-pool2-2.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\projectlombok\lombok\1.16.18\lombok-1.16.18.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\springframework\session\spring-session-data-redis\1.2.5.RELEASE\spring-session-data-redis-1.2.5.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\redisson\redisson\2.9.0\redisson-2.9.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\io\netty\netty-common\4.1.8.Final\netty-common-4.1.8.Final.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\io\netty\netty-codec\4.1.8.Final\netty-codec-4.1.8.Final.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\io\netty\netty-buffer\4.1.8.Final\netty-buffer-4.1.8.Final.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\io\netty\netty-transport\4.1.8.Final\netty-transport-4.1.8.Final.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\io\netty\netty-resolver\4.1.8.Final\netty-resolver-4.1.8.Final.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\io\netty\netty-handler\4.1.8.Final\netty-handler-4.1.8.Final.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\javax\cache\cache-api\1.0.0\cache-api-1.0.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\io\projectreactor\reactor-stream\2.0.8.RELEASE\reactor-stream-2.0.8.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\io\projectreactor\reactor-core\2.0.8.RELEASE\reactor-core-2.0.8.RELEASE.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\reactivestreams\reactive-streams\1.0.0\reactive-streams-1.0.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\fasterxml\jackson\dataformat\jackson-dataformat-yaml\2.6.7\jackson-dataformat-yaml-2.6.7.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\yaml\snakeyaml\1.15\snakeyaml-1.15.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\fasterxml\jackson\core\jackson-core\2.6.7\jackson-core-2.6.7.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\fasterxml\jackson\core\jackson-databind\2.6.7\jackson-databind-2.6.7.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\fasterxml\jackson\dataformat\jackson-dataformat-avro\2.9.0\jackson-dataformat-avro-2.9.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\apache\avro\avro\1.8.1\avro-1.8.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\thoughtworks\paranamer\paranamer\2.7\paranamer-2.7.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\xerial\snappy\snappy-java\1.1.1.3\snappy-java-1.1.1.3.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\apache\commons\commons-compress\1.8.1\commons-compress-1.8.1.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\org\tukaani\xz\1.5\xz-1.5.jar;F:\xiangmu3\Xin\Idea\design_Maven_Home\lib\com\sun\faces\jsf-api\1.2\jsf-api-1.2.jar" com.geely.design.pattern.structural.adapter.classadapter.Test
目标实现类自己的方法,,,
被适配器执行它自己的方法。。。

Process finished with exit code 0

 

 

2.2  代码演练2(对象适配模式)

需求:a类和b类,想用b类或者b类的子类实现a类的方法

 

设计分析:

通过组合达到目的

 

关联关系:

只有 适配类发生了变化,其他类都不变。

 

UML类图:

 

 

适配器类:

package com.geely.design.pattern.structural.apapter.objectadapter;

public class Adapter implements Target {
    Adaptee adaptee = new Adaptee();

    @Override
    public void request() {
        adaptee.adapteeRequest();
    }
}

 

测试类:

package com.geely.design.pattern.structural.apapter.objectadapter;

public class Test {
    public static void main(String [] args){
        //打印目标类的方法
        Target target = new ConcreteTarget();
        target.request();


        //打印适配器的方法
        Adapter adapter = new Adapter();
        adapter.request();

    }
}

 

目标接口:

package com.geely.design.pattern.structural.apapter.objectadapter;

/**
 * 目标接口
 */
public interface Target {
    void request();
}

 

目标实现类:

package com.geely.design.pattern.structural.apapter.objectadapter;

/**
 * 目标实现类
 */
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        System.out.println("目标实现类自己的方法,,,");
    }
}

 

被适配类:

package com.geely.design.pattern.structural.apapter.objectadapter;

/**
 * 该类是被适配者,想实现目标类的方法
 */
public class Adaptee {
    public void adapteeRequest(){
        System.out.println("被适配器执行它自己的方法。。。");
    }
}

 

打印日志:

目标实现类自己的方法,,,
被适配器执行它自己的方法。。。

Process finished with exit code 0

 

2.3  代码演练3(具体应用场景)

需求:

手机电源适配器,把220V交流电转化为5V直流电

 

测试类:

package com.geely.design.pattern.structural.adapter;

public class Test {
    public static void main(String [] args){
        DC5V dc5V = new PowerAdapter();
        dc5V.outPutDC5V();
    }
}

 

电源适配器类:

package com.geely.design.pattern.structural.adapter;

import com.geely.design.pattern.structural.adapter.AC220V;
import com.geely.design.pattern.structural.adapter.DC5V;

/**
 * 电源适配器
 */
public class PowerAdapter implements DC5V {

    //通过组合引入交流电220V
    AC220V ac220V = new AC220V();

    @Override
    public int outPutDC5V() {
        int adapterInput = ac220V.outputAC220V();

        //变压器转换
        int adapterOutput = adapterInput/44;

        System.out.println("使用电源适配器,最终转换为直流电"+adapterOutput+"V");
        return adapterOutput;
    }
}

 

目标类直流电5V类:

package com.geely.design.pattern.structural.adapter;

public interface DC5V {
    //输出5V直流电
    int outPutDC5V();
}

 

被适配器交流电220V类:

package com.geely.design.pattern.structural.adapter;

public class AC220V {
    public int outputAC220V(){
        int outputELE = 220;
        System.out.println("输出交流电"+outputELE+"V");
        return outputELE;
    }
}

 

打印日志:

输出交流电220V
使用电源适配器,最终转换为直流电5V

Process finished with exit code 0

 

posted on 2019-09-10 05:52  菜鸟乙  阅读(223)  评论(0编辑  收藏  举报