zoukankan      html  css  js  c++  java
  • Java基础知识之设计模式--适配器模式

    Java设计模式--适配器模式

    声明:本文根据慕课网tuohuangs老师的精品课程整理来的:慕课网

    什么是设计模式(Design Pattern)?

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

    适配器模式的定义?

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

    适配器模式实现方式?

      1.使用组合方式实现:把“被适配者”作为一个对象组合到适配器类中,以修改目标接口包装被适配者。

      2.使用继承方式实现:通过多重继承不兼容接口,实现对目标接口的匹配,单一的为某个类而实现适配。

    适配器模式的作用:

      1.透明:通过适配器,客户端可以调用同一接口,因而对客户端来说是透明的。这样做更简单,更直接,更紧凑。

      2.重用:复用了现存的类,解决了现存类和复用环境要求不一致的问题。

      3.低耦合:将目标类和适配者类解耦,通过引入一个适配器类重用现有的适配者类,而无需修改原代码,(遵循开闭原则)。

    适配器模式实例:

      1.首先创建一个目标接口

    1 /**
    2  * 目标接口类(三相插座接口)
    3  * @author Administrator
    4  *
    5  */
    6 public interface ThreePlugIf {
    7     //目标方法(使用三相电流供电)
    8     public void powerWhitThree();
    9 }

      2.创建一个原有类,也就是不匹配类

     1 /**
     2  * 原始类,不匹配类
     3  * @author Administrator
     4  *
     5  */
     6 public class GBTwoPlug {
     7     
     8     //使用二相电流供电
     9     public void powerWithTwo(){
    10         System.out.println("使用二相电流供电");
    11     }
    12 }

      3.创建客户端,规定适配类

    /**
     * 客户端类,明确使用三相插头
     * @author Administrator
     *
     */
    public class NoteBook {
    
        private ThreePlugIf plug;
        //创建构造方法,明确使用三相插头
        public NoteBook(ThreePlugIf plug) {
            this.plug = plug;
        }
        //使用三相插座充电
        public void charge(){
            plug.powerWhitThree();
        }
    
    }

      4.创建组合适配器

     1 /**
     2  * 适配器,把二相插头转换成三相插头
     3  * @author Administrator
     4  *
     5  */
     6 public class TwoPlugAdapter implements ThreePlugIf {
     7     //需要转换的类
     8     private GBTwoPlug twoPlug;
     9     
    10     public TwoPlugAdapter(GBTwoPlug twoPlug) {
    11         this.twoPlug = twoPlug;
    12     }
    13     @Override
    14     public void powerWhitThree() {
    15         System.out.println("通过转换");
    16         twoPlug.powerWithTwo();
    17     }
    18 
    19 }

      5.组合适配器测试类

     1 public class Test {
     2 
     3     public static void main(String[] args) {
     4         GBTwoPlug twoPlug = new GBTwoPlug();
     5         //需要使用适配器把二相的转换成三相的
     6         ThreePlugIf threePlug = new TwoPlugAdapter(twoPlug);
     7         NoteBook noteBook = new NoteBook(threePlug);
     8         noteBook.charge();
     9     }
    10 
    11 }

      6.创建继承适配器

     1 /**
     2  * 采用继承方式的插座适配器
     3  * @author Administrator
     4  *
     5  */
     6 public class TwoPlugAdapterExtends extends GBTwoPlug implements ThreePlugIf {
     7 
     8     @Override
     9     public void powerWhitThree() {
    10         System.out.println("借助继承适配器");
    11         //使用继承父类中的方法
    12         this.powerWithTwo();
    13 
    14     }
    15 
    16 }

      7.继承适配器测试类

     1 public class Test1 {
     2 
     3     public static void main(String[] args) {
     4         //创建继承测试类
     5         ThreePlugIf threePlug = new TwoPlugAdapterExtends();
     6         NoteBook noteBook = new NoteBook(threePlug);
     7         noteBook.charge();
     8     }
     9 
    10 }
  • 相关阅读:
    【心情】12月22日
    【心情】12月12日
    【心情】12月8日
    【转载】信息学竞赛知识地图
    SharePoint 2013 工作流之年假审批Designer配置篇
    SharePoint 2013 状态机工作流之扩展自定义状态
    SharePoint 2013 状态机工作流之UpdateItemActivity
    SharePoint 2013 状态机工作流之日常报销示例
    SharePoint 2013 对二进制大型对象(BLOB)进行爬网
    SharePoint 2013 图文开发系列之应用程序页
  • 原文地址:https://www.cnblogs.com/wk-missQ1/p/12323401.html
Copyright © 2011-2022 走看看