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來說要更有用一些,代碼很簡單,不解釋,你們懂的。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章