结构型模式-适配器模式(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)_第1张图片

墙上的插座是三相的,而我手头的电器(如吹风机是两相的),那么我们就需要一个三相转两相的一个插头.

这个三相转两相的转换器就叫Adapter,对已有的接口不符合我们需要的接口时,写一个转换的接口,来完成对接功能.

结构型模式-适配器模式(Adapter)_第2张图片

结构型模式-适配器模式(Adapter)_第3张图片

  

/**/ /*
 * 已有接口
 
*/

public   class  Util  ... {

    
public static void printCollection(Iterator iter)
    
...{
        
while(iter.hasNext())
        
...{
            System.out.println(iter.next());
        }

    }

}


public   class  Client  ... {

    
public static void 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
 
*/

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

    
public boolean hasNext() ...{
        
        
return enumeration.hasMoreElements();
    }

    
public Object next() ...{

        
return enumeration.nextElement();
    }

    
public void remove() ...{
        
    }

}

 

          

三:结构

     结构型模式-适配器模式(Adapter)_第4张图片

adapter class

结构型模式-适配器模式(Adapter)_第5张图片

adapter object

四:实际应用

  1.  

 

五:优缺点

参考文献:
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.

二:引入

假设现在有一家咖啡店,经营咖啡,茶等饮料,我们来为它设计一个系统。

 

结构型模式-适配器模式(Adapter)_第6张图片

问题:

  • 饮料加糖或牛奶等调料时是要另收费的(每包1元),顾客可以要也可以不要,所以这个固定价格的cost方法不符合要求. 不符合OCP(open for extension,close for modification)

重新设计:

结构型模式-适配器模式(Adapter)_第7张图片

很显然,这种设计也不合适,会导致

  • 类数量爆炸性增长(class exploson).
  • 也不符合要求,比如有的顾客要两包糖等情况没有考虑到.

引入Decorator设计模式:

public   abstract   class  Beverage  ... {
    
private String discription;

    
    
public String getDiscription() ...{
        
return discription;
    }


    
public void setDiscription(String discription) ...{
        
this.discription = discription;
    }


    
public abstract double cost() ;
}



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

    
public  double cost() 
    
...{
        
return 10;
    }

}



public   abstract   class  CondimentDecorator  extends  Beverage ... {
    
public abstract String getDiscription() ;
}


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

    
public  double cost() 
    
...{
        
return beverage.cost()+1;
    }

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

}



public   class  Client  ... {
    
public static void 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());
    }

}



 

三:结构

     结构型模式-适配器模式(Adapter)_第8张图片

四:实际应用

  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

     

你可能感兴趣的:(设计模式,出版)