java设计模式之适配器

前摘: 

  适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。

用途:

  看过一篇文章写过这样一句话:"比如你有一个插座,只有两个孔,但你的吹风机有三个插头,你刚洗了头发,怎么办?很好处理,找个三个孔两个插头的插座插到这两个孔的插座上就可以吹头发了!"

结构:

  适配器模式有两种:一种是类的适配器模式,另外一种是对象的适配器模式。

  类的适配器模式:

     

     

  通过上图可已看出,Adaptee没有doSomething2()方法,然后客户端期待拥有该方法,为了让客户端拥有这个类。提供一个中间环节,即类Adapter,把Adaptee的API与Target类的API衔接起来。Adapter与Adaptee是继承关系,这决定了这个适配器模式是类的:

  模式所涉及的角色有:

  ●  目标(Target)角色:这就是所期待得到的接口。注意:由于这里讨论的是类适配器模式,因此目标不可以是类。

  ●  源(Adapee)角色:现在需要适配的接口。

  ●  适配器(Adaper)角色:适配器类是本模式的核心。适配器把源接口转换成目标接口。显然,这一角色不可以是接口,而必须是具体类。

    代码:

      目标:

1

2

3

4

public interface AdapterInterface {

  void doSomething1();

  void doSomething2();

}

  

         源:

1

2

3

4

5

6

7

8

9

10

public class Adaptee {

     

     

  public void doSomething1(){

      System.out.println("doSomething1");

  }

  public void doSomething3(){

      System.out.println("doSomething3");

  }

}

  

   适配器:  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public class Adapter extends Adaptee implements AdapterInterface {

 

     

    public static void main(String[] args){

        AdapterInterface s = new Adapter();

        s.doSomething1();

        s.doSomething2();

    }

 

 

    @Override

    public void doSomething2() {

        // TODO Auto-generated method stub

        System.out.println("doSomething2");

    }

}

 

   输出结果:

      doSomething1
      doSomething2

    对象适配器模式:

   

    从上图可知,与前面的类设配器模式不同的是,类适配器模式是需要继承源来实现接口,而对象适配器不然,它直接把源作为适配器本身的一个属性来实现目标,满足客户端的期待。

    源代码:

     目标:

1

2

3

4

public interface AdapterInterface {

  void doSomething1();

  void doSomething2();

}

  源:

1

2

3

4

5

6

7

8

9

10

public class Adaptee {

     

     

  public void doSomething1(){

      System.out.println("doSomething1");

  }

  public void doSomething3(){

      System.out.println("doSomething3");

  }

}

 适配器:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

public class Adapter implements AdapterInterface {

     

    private Adaptee adaptee;

     

    public Adapter(Adaptee adaptee){

        this.adaptee = adaptee;

    }

     

    public static void main(String[] args){

        AdapterInterface s = new Adapter(new Adaptee);

        s.doSomething1();

        s.doSomething2();

    }

 

 

    @Override

    public void doSomething2() {

        System.out.println("doSomething2");

    }

 

 

    @Override

    public void doSomething1() {

        this.adaptee.doSomething1();               this.adaptee.doSomething3();

    }

}

  通过上述代码,可以很清晰的看到对象适配器和类适配器之间的不同点:

    ●  类适配器使用对象继承的方式,是静态的定义方式;而对象适配器使用对象组合的方式,是动态组合的方式。

    ●  对于类适配器,由于适配器直接继承了Adaptee,使得适配器不能和Adaptee的子类一起工作,因为继承是静态的关系,当适配器继承了Adaptee后,就不可能再去处理  Adaptee的子类了。

  ●   对于类适配器,适配器可以重定义Adaptee的部分行为,相当于子类覆盖父类的部分实现方法。

     对于对象适配器,要重定义Adaptee的行为比较困难,这种情况下,需要定义Adaptee的子类来实现重定义,然后让适配器组合子类。虽然重定义Adaptee的行为比较困难,但是想要增加一些新的行为则方便的很,而且新增加的行为可同时适用于所有的源。

  ●   对于类适配器,仅仅引入了一个对象,并不需要额外的引用来间接得到Adaptee。

     对于对象适配器,需要额外的引用来间接得到Adaptee。

  适配器模式的缺点

  过多的使用适配器,会让系统非常零乱,不易整体进行把握。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。

缺省适配模式

  缺省适配(Default Adapter)模式为一个接口提供缺省实现,这样子类型可以从这个缺省实现进行扩展,而不必从原有接口进行扩展。作为适配器模式的一个特例,缺省是适配模式在JAVA语言中有着特殊的应用。

      源代码: 

         接口: 

1

2

3

4

public interface AdapterInterface {

  void doSomething1();

  void doSomething2();

}

  适配器抽象类:

1

2

3

4

5

6

7

8

public abstract class Adapter implements AdapterInterface {

    public void doSomething1(){

        System.out.println("doSomething1");

    }

    public void doSomething2(){

        System.out.println("doSomething2");

    }

}

  继承抽象类接口功能部分实现:

1

2

3

4

5

6

7

8

9

10

11

public class AdapterImpl extends Adapter{

    

    public static void main(String arg[]){

      AdapterInterface ai = new AdapterImpl();

      ai.doSomething1();

   }

    

   public void doSomething2(){

       super.doSomething2();

   }

}

  如若不想实现接口的所有方法,那通过这种抽象类适配器的缺省模式可以很好地解决问题,给出所有方法的平庸的具体实现。

      如此,从这个抽象类实现的子类不用实现接口的所有方法了~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值