1. 程式人生 > >結構型模式-介面卡模式(Adapter)

結構型模式-介面卡模式(Adapter)

一:定義:

Adapter:Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

二:引入

先來看張圖:

牆上的插座是三相的,而我手頭的電器(如吹風機是兩相的),那麼我們就需要一個三相轉兩相的一個插頭.

這個三相轉兩相的轉換器就叫Adapter,對已有的介面不符合我們需要的介面時,寫一個轉換的介面,來完成對接功能.

/*
 * 已有介面
 
*/

publicclass Util {

    
publicstaticvoid printCollection(Iterator iter)
    
{
        
while(iter.hasNext())
        
{
            System.out.println(iter.next());
        }

    }

}


publicclass Client {

    
publicstaticvoid main(String[] args) {

        List list
=new ArrayList();
        list.add(
"L1");
        list.add(
"L2");
        list.add(
"L3");
        Util.printCollection(list.iterator());
        
        Vector vct
=new Vector();
        vct.add(
"v1");
        vct.add(
"v2");
        vct.add(
"v3");
        
//Util.printCollection(vct.elements()); 不符合介面
        Util.printCollection(new EnumerationIterator(vct.elements()));            
    }

}


/*
 * Adapter--Adapter,Iterator 是target,目標是要轉成Iterator
 
*/

publicclass EnumerationIterator implements Iterator{
    
private Enumeration enumeration;
    
//Enumeration為Adaptee
public EnumerationIterator(Enumeration enumeration)
    
{
        
this.enumeration=enumeration;
    }

    
publicboolean hasNext() {
        
        
return enumeration.hasMoreElements();
    }

    
public Object next() {

        
return enumeration.nextElement();
    }

    
publicvoid remove() {
        
    }

}

三:結構

    

adapter class

adapter object

四:實際應用

五:優缺點

參考文獻:
1:閻巨集,《Java與模式》,電子工業出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY

    

一:定義:

Decorator:Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

二:引入

假設現在有一家咖啡店,經營咖啡,茶等飲料,我們來為它設計一個系統。

問題:

  • 飲料加糖或牛奶等調料時是要另收費的(每包1元),顧客可以要也可以不要,所以這個固定價格的cost方法不符合要求. 不符合OCP(open for extension,close for modification)

重新設計:

很顯然,這種設計也不合適,會導致

  • 類數量爆炸性增長(class exploson).
  • 也不符合要求,比如有的顧客要兩包糖等情況沒有考慮到.

引入Decorator設計模式:

publicabstractclass Beverage {
    
private String discription;

    
    
public String getDiscription() {
        
return discription;
    }


    
publicvoid setDiscription(String discription) {
        
this.discription = discription;
    }


    
publicabstractdouble cost() ;
}



publicclass Coffee extends Beverage{
    
public Coffee()
    
{
        setDiscription(
"coffee");
    }

    
publicdouble cost() 
    
{
        
return10;
    }

}



publicabstractclass CondimentDecorator extends Beverage{
    
publicabstract String getDiscription() ;
}


publicclass CondimentMilk extends CondimentDecorator{
    
private Beverage beverage;
    
public CondimentMilk(Beverage beverage)
    
{
        
this.beverage=beverage;
    }

    
publicdouble cost() 
    
{
        
return beverage.cost()+1;
    }

    
public  String getDiscription() 
    
{
        
return beverage.getDiscription()+",milk";
    }

}



publicclass Client {
    
publicstaticvoid main(String args[])
    
{
        Beverage coffee
=new Coffee();
        System.out.println(coffee.getDiscription()
+":"+coffee.cost());
        System.out.println(
new CondimentMilk(coffee).getDiscription()+":"+new CondimentMilk(coffee).cost());
        System.out.println(
new CondimentSugar(new CondimentMilk(coffee)).getDiscription()+":"+new CondimentSugar( new CondimentMilk(coffee)).cost());
        System.out.println(
new CondimentSugar(new CondimentSugar(new CondimentMilk(coffee))).getDiscription()+":"+new CondimentSugar(new CondimentSugar( new CondimentMilk(coffee))).cost());
    }

}



三:結構

    

四:實際應用

  1. JAVA IO

五:適用情形

Use the Adapter pattern when

you want to use an existing class, and its interface does not match the one you need. you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces. (object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.

參考文獻:
1:閻巨集,《Java與模式》,電子工業出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY