Adapter模式

作者: 吃啥呀 | 来源:发表于2018-07-15 23:13 被阅读14次

java设计模式

eg.png
插座的比喻 实例程序
需求 两孔插座 Two接口
交换装置 适配器 Adapter类
实际接口 三孔插座 Three接口
实际实现 三孔实现类 Three类

类适配器模式(使用继承)

接口A中没有我们想要的方法 ,接口B中有合适的方法,不能改变访问接口A
定义一个适配器p:继续访问当前接口A中的方法
p继承接口B的实现类BB : 可以在适配器P中访问接口B的方法
在适配器P中的接口A方法中直接引用BB中的合适方法


//Two接口Ithree:
public interface Ithree{
     void isthree();
 }

//Three接口Itwo:
public interface Itwo{
     void istwo();
 }

//Ithree接口的实现类
public class Three implements Ithree{
     @Override
     public void isthree() {
         System.out.println("Three");
     }
 }

//适配器adapter
public class Adapter extends Three implements Itwo { 
     @Override
     public void istwo() {
         isthree();
     }
}

//测试方法:Clienter
public class Clienter { 
     public static void main(String[] args) {
         Two two = new Adapter();
         two.istwo();
     }
}

对象适配器模式

要访问的接口A中没有想要的方法 ,却在接口B中发现了合适的方法,接口A不能改变
定义一个适配器p:作用:实现我们访问的接口A
P中定义私有变量C(对象)(B接口的实现类)
再定义一个带参数的构造器用来为对象C赋值
再在A接口的方法实现中使用对象C调用其来源于B接口的方法。

//Two接口Ithree:
public interface Ithree{
     void isthree();
 }

//Three接口Itwo:
public interface Itwo{
     void istwo();
 }

//Ithree接口的实现类
public class Three implements Ithree{
     @Override
     public void isthree() {
         System.out.println("Three");
     }
 }

//适配器adapter
public class Adapter implements Itwo {   
      private Three three;
      public Adapter(Three three){
          this.three = three;
      }
      @Override
      public void istwo() {
          three.isthree();
     } 
}

//测试方法:Clienter
public class Clienter { 
     public static void main(String[] args) {
         Two two = new Adapter();
         two.istwo();
     }
}

接口适配器模式

当存在这样一个接口,其中定义了N多的方法,却只想使用其中的一个到几个方法,如果我们直接实现接口,那么我们要对所有的方法进行实现,哪怕我们仅仅是对不需要的方法进行置空(只写一对大括号,不做具体方法实现)也会导致这个类变得臃肿,调用也不方便
使用一个抽象类作为中间件——适配器:实现接口,抽象类中所有的方法都进行置空
再创建抽象类的继承类
重写我们需要使用的那几个方法

//目标接口A
public interface A {
     void a();
     void b();
     void c();
     void d();
     void e();
     void f();
}

//适配器:Adapter
public abstract class Adapter implements A {
     public void a(){}
     public void b(){}
     public void c(){}
     public void d(){}
     public void e(){}
     public void f(){}
}

//实现类:Asuccess
public clall Asuccess extends Adapter{
    public void a(){
        System.out.println("实现A方法被调用");
    }
}

//测试类
public class Clienter { 
     public static void main(String[] args) {
         A a = new Ashili();
         a.a();
         a.d();
     } 
}

总结

adapter.png

相关文章

  • 11.3设计模式-适配器-详解

    设计模式-适配器adapter模式 adapter模式详解 adapter模式在android中的实际运用1.li...

  • 浅谈设计模式之适配器模式

    适配器模式(Adapter Pattern) 概述: 在设计模式中,适配器模式(adapter pattern)有...

  • Adapter模式

    java设计模式 类适配器模式(使用继承) 接口A中没有我们想要的方法 ,接口B中有合适的方法,不能改变访问接口A...

  • Adapter模式

    一、概述 将类的接口转换为客户端期望的另一个接口。 二、使用 2.1UML结构图

  • Adapter模式

    分为类模式和对象两种 类模式public继承接口,private继承实现 Adapter.h #ifndef _A...

  • 图解设计模式Adapter模式

    Adapter(适配器模式) 适配器模式用于填补现有程序和所需程序之间的差异 Adapter模式有以下两种 类适配...

  • 浅谈GoF23设计模式-“Adapter”模式

    “Adapter”模式为结构型设计模式,C#当中主要使用对象适配器。“Adapter”模式定义:将一个类的接口转换...

  • 适配器模式

    适配器模式简介 适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使...

  • 面向对象编程设计模式------适配器模式

    适配器模式   适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原...

  • 适配器模式(Adapter模式)详解

    模式的定义 适配器模式(Adapter)将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使得原本由...

网友评论

    本文标题:Adapter模式

    本文链接:https://www.haomeiwen.com/subject/kmlmpftx.html