zhoumin007 发表于 2013-2-5 01:34:30

结构型模式---装饰模式(Decorator)

一:定义:
Decorator:Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
二:引入
假设现在有一家咖啡店,经营咖啡,茶等饮料,我们来为它设计一个系统。
 
http://p.blog.csdn.net/images/p_blog_csdn_net/zhoumin008/decorator1.jpg
问题:
   
[*]饮料加糖或牛奶等调料时是要另收费的(每包1元),顾客可以要也可以不要,所以这个固定价格的cost方法不符合要求. 不符合OCP(open for extension,close for modification)
重新设计:
http://p.blog.csdn.net/images/p_blog_csdn_net/zhoumin008/p2.jpg
很显然,这种设计也不合适,会导致
   
[*]类数量爆炸性增长(class exploson).   
[*]也不符合要求,比如有的顾客要两包糖等情况没有考虑到.
引入Decorator设计模式:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic abstract class Beverage ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private String discription;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public String getDiscription() ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return discription;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public void setDiscription(String discription) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        this.discription = discription;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public abstract double cost() ;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic class Coffee extends Beverage...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public Coffee()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        setDiscription("coffee");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public  double cost() 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return 10;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic abstract class CondimentDecorator extends Beverage...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public abstract String getDiscription() ;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic class CondimentMilk extends CondimentDecorator...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private Beverage beverage;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public CondimentMilk(Beverage beverage)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        this.beverage=beverage;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public  double cost() 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return beverage.cost()+1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public  String getDiscription() 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return beverage.getDiscription()+",milk";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic class Client ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public static void main(String args[])
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Beverage coffee=new Coffee();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(coffee.getDiscription()+":"+coffee.cost());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(new CondimentMilk(coffee).getDiscription()+":"+new CondimentMilk(coffee).cost());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(new CondimentSugar(new CondimentMilk(coffee)).getDiscription()+":"+new CondimentSugar( new CondimentMilk(coffee)).cost());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(new CondimentSugar(new CondimentSugar(new CondimentMilk(coffee))).getDiscription()+":"+new CondimentSugar(new CondimentSugar( new CondimentMilk(coffee))).cost());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页: [1]
查看完整版本: 结构型模式---装饰模式(Decorator)