JAVA设计模式-Adapter

有些时候,我们想要使用一些类的某个功能,但是发现这个类提供的接口不符合我们的要求,这个时候就需要让这个接口符合要求,最直接想到的办法就是去改这个接口,但是这个办法明显违背了面向对象的原则。所以,为了解决这个办法,就提出了Adapter模式。Adapter模式分为两种,一种是类Adapter,一种是对象Adapter,后面将对这两种方法进行详细的解释。

 

    首先是类Adapter。

 

   

  1. interface Operation{  
  2.     public int add(int a,int b);  
  3. }  
  4. class Calculater{  
  5.     public int otherAdd(int a,int b){  
  6.         return a + b;  
  7.     }  
  8. }  
  9. class OperationAdapter extends Calculater implements Operation{  
  10.     @Override  
  11.     public int add(int a, int b) {  
  12.         // TODO Auto-generated method stub   
  13.         return otherAdd(a,b);  
  14.     }     
  15. }  
  16. public class Test{  
  17.     public static void main(String[] args){  
  18.         OperationAdapter adapter = new OperationAdapter();  
  19.         int x = 10;  
  20.         int y = 20;  
  21.         int sum = adapter.add(x, y);  
  22.         System.out.println(x + " + " + y + " = " + sum);  
  23.     }  
  24. }  

 

    上面的代码中,OperationAdapter这个类将Calculater的add()方法适配到Operation这个类里面,OperationAdapter就是一个适配器。这样做有一个问题,当适配器需要从多个类里面调用方法的时候,类Adapter方法就不适用了,因为java不允许多重继承,既然不能用继承,那么就用组合好了,因此,就有了对象Adapter。

 

   

  1. package adapter.objectAdapter;  
  2.   
  3. interface Operation{  
  4.     public int add(int a,int b);  
  5.     public int minus(int a,int b);  
  6.     public int multiplied(int a,int b);  
  7. }  
  8. class OtherAdd{  
  9.     public int otherAdd(int a,int b){  
  10.         return a + b;  
  11.     }  
  12. }    
  13.   
  14. class OtherMinus{  
  15.     public int minus(int a,int b){  
  16.         return a - b;  
  17.     }  
  18. }    
  19.   
  20. class OtherMultiplied{  
  21.     public int multiplied(int a,int b){  
  22.         return a * b;  
  23.     }  
  24. }  
  25. class OperationAdapter implements Operation{  
  26.     private OtherAdd add;  
  27.     private OtherMinus minus;  
  28.     private OtherMultiplied multiplied;  
  29.   
  30.     public OperationAdapter(){  
  31.         add = new OtherAdd();  
  32.         minus = new OtherMinus();  
  33.         multiplied = new OtherMultiplied();  
  34.     }  
  35.   
  36.     @Override  
  37.     public int add(int a, int b) {  
  38.         // TODO Auto-generated method stub   
  39.         return add.otherAdd(a,b);  
  40.     }  
  41.   
  42.     @Override  
  43.     public int minus(int a, int b) {  
  44.         // TODO Auto-generated method stub   
  45.         return minus.minus(a,b);  
  46.     }  
  47.   
  48.     @Override  
  49.     public int multiplied(int a, int b) {  
  50.         // TODO Auto-generated method stub   
  51.         return multiplied.multiplied(a,b);  
  52.     }  
  53. }  
  54. public class Test{  
  55.     public static void main(String[] args){  
  56.         OperationAdapter adapter = new OperationAdapter();  
  57.         int x = 10;  
  58.         int y = 20;  
  59.         System.out.println(x + " + " + y + " = " + adapter.add(x, y));  
  60.         System.out.println(x + " - " + y + " = " + adapter.minus(x, y));  
  61.         System.out.println(x + " * " + y + " = " + adapter.multiplied(x, y));  
  62.     }  
  63. }  

 

    总的来说,对象Adapter比起类Adapter来说要更有用一些,代码很简单,不解释,你们懂的。

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值