码迷,mamicode.com
首页 > 其他好文 > 详细

装饰者模式

时间:2014-06-05 03:28:12      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:装饰者模式   c++   设计   

装饰者模式

Decorator模式(别名Wrapper):动态将职责附加到对象上,若要扩展功能,装饰者提供了比继承更具弹性的代替方案。

 

意图:

动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。

 

设计原则:

1. 多用组合,少用继承。

利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而,如果能够利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。

2. 类应设计的对扩展开放,对修改关闭。

 

要点:

1. 装饰者和被装饰对象有相同的超类型。

2. 可以用一个或多个装饰者包装一个对象。

3. 装饰者可以在所委托被装饰者的行为之前或之后,加上自己的行为,以达到特定的目的。

4. 对象可以在任何时候被装饰,所以可以在运行时动态的,不限量的用你喜欢的装饰者来装饰对象。

5. 装饰模式中使用继承的关键是想达到装饰者和被装饰对象的类型匹配,而不是获得其行为。

6. 装饰者一般对组件的客户是透明的,除非客户程序依赖于组件的具体类型。在实际项目中可以根据需要为装饰者添加新的行为,做到“半透明”装饰者。

7. 适配器模式的用意是改变对象的接口而不一定改变对象的性能,而装饰模式的用意是保持接口并增加对象的职责。



#include <iostream>

#include<cstdlib>
#include <algorithm>
#include<hash_map>
#include <vector>
#include <sstream>
#include<string>
using namespace std;
//enum condiment{DarkRost,Decat,Espresso,HouseBlend,Mocha,Soy,Whip};
class Beverage
{
    public:
    string description;
    /*
    hashmap<string,int> hash;
    Beverage()
    {
        hash[condiment.Mocha]=0;
        hash[condiment.Soy]=1;
        hash[condiment.Whip]=2;
        hash[condiment.DarkRost]=-1;
        hash[condiment.Decat]=-1;
        hash[condiment.Espresso]=-1;
        hash[condiment.HouseBlend]=-1;
    }*/
    int size;//
    int getSize()
    {
        return size;
    }
    void setSize(int Size)
    {
        size=Size;
    }
   virtual string  getDescription()
    {
        return description;
    }
     virtual double cost()=0;
};
 class Espresso :public Beverage
{
    public :
    Espresso()
    {
        description="Espresso";
    }
     double cost()
    {
        return 1.99;
    }
};
class HouseBlend:public Beverage
{
    public :
    HouseBlend()
    {
        description="House Blend Coffee";
    }
    double cost()
    {
        return 0.89;
    }
};
class DarkRost:public Beverage
{
    public :
    DarkRost()
    {
        description="DarkRost";
    }
    double cost()
    {
        return 0.99;
    }


};
class Decat:public Beverage
{
    public:
    Decat()
    {
        description="Decat";
    }
    double cost()
    {
        return 1.05;
    }
};
class CondimentDecorator:public Beverage
{
    virtual string getDescription()=0;
};
class Mocha:public CondimentDecorator
{
    Beverage *bEverage;
    public:
    Mocha(Beverage *beverage)
    {
        bEverage=beverage;
    }
    string getDescription()
    {
        return bEverage->getDescription()+",Mocha";
    }
    double cost()
    {
        return 0.20+bEverage->cost();
    }
};
class Whip:public CondimentDecorator
{
     Beverage *bEverage;
    public:
    Whip(Beverage *beverage)
    {
        bEverage=beverage;
    }
    string getDescription()
    {
        return bEverage->getDescription()+",Whip";
    }
    double cost()
    {
        return 0.10+bEverage->cost();
    }
};
class Soy:public CondimentDecorator
{
     Beverage *bEverage;
public:
  Soy(Beverage *beverage)
    {
        bEverage=beverage;
    }
    string getDescription()
    {
        return bEverage->getDescription()+",Soy";
    }
    int getSize()
    {
        return bEverage->getSize();
    }


    double cost()
    {
        double cost=0.15+bEverage->cost();
        if(getSize()==0)
        {
            cost+=0.10;
        }
        else if(getSize()==1)
        {
            cost+=0.15;
        }
        else
        {
            cost+=0.20;
        }
        return cost;




    }
};
class Dictionary
{
    public:
    string change(string a)
    {
        vector<string> l;
        stringstream ss(a);//stringstream由iostream派生而来提供读写string功能
        //创建存储a副本 stringstream 对象其str string 类型对象
        string s;
        while (getline(ss, s, ‘,‘)) {
            l.push_back(s);
            }//getline 函数用法遇到,号时停止,并把ss中的字符串写到s中
        sort(l.begin()+1,l.end());
        string r;
        for(vector<string>::iterator it=l.begin();it!=l.end();it++)
        {
            if(it!=l.begin())
            {
                r=r+‘,‘;
            }
            r+=*it;
        }
        return r;
    }
};
int main()
{
    Beverage *b=new Espresso();
    Dictionary d;
    cout<<b->getDescription()<<" $ "<<b->cost()<<endl;
    Beverage *b1=new DarkRost();
    b1=new Mocha(b1);
    b1=new Mocha(b1);
    b1=new Whip(b1);
    cout<<d.change(b1->getDescription())<<" $ "<<b1->cost()<<endl;
    Beverage *b2=new HouseBlend();
    b2->setSize(2);
    b2=new Soy(b2);
    b2=new Mocha(b2);
    b2=new Whip(b2);
    cout<<d.change(b2->getDescription())<<" $ "<<b2->cost()<<endl;
    //cout<<d.change("yells,hello,jil,sdj,sdk,zkl,wiei,woweo");
    return 0;
}

装饰者模式,布布扣,bubuko.com

装饰者模式

标签:装饰者模式   c++   设计   

原文地址:http://blog.csdn.net/lv836735240/article/details/27216317

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!