随笔-91  评论-137  文章-0  trackbacks-0

装饰模式

前序

制作一个可以给人搭配不同的服饰的系统,比如类似QQ,网络游戏或论坛都有的Avatar系统.

装饰模式

装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案,提供比继承更多的灵活性。动态给一个对象增加功能,这些功能可以再动态的撤消。增加由一些基本功能的排列组合而产生的非常大量的功能。

实现方式(UML类图)

实现代码

#include <stdio.h>

 

class Person

{

public:

       Person() : name(0){}

       Person(char* _name) : name(_name){}

      

       virtual void Show()

       {

              printf("装扮的%s",name);

       }

protected:

       char* name;

};

 

class Finery : public Person

{

public:

       Finery() : component(0){}

      

       void Decorate(Person* component)

       {

              this->component = component;

       }

      

       virtual void Show()

       {

              if(component) component->Show();

       }

protected:

       Person* component;

};

 

class TShirts : public Finery

{

public:

       virtual void Show()

       {

              printf("T ");

              __super::Show();

       }

};

 

class BigTrouser : public Finery

{

public:

       virtual void Show()

       {

              printf("跨裤 ");

              __super::Show();

       }

};

 

class Sneakers : public Finery

{

public:

       virtual void Show()

       {

              printf("破球鞋 ");

              __super::Show();

       }

};

 

class Suit : public Finery

{

public:

       virtual void Show()

       {

              printf("西装 ");

              __super::Show();

       }

};

 

class Tie : public Finery

{

public:

       virtual void Show()

       {

              printf("领带 ");

              __super::Show();

       }

};

 

class LeatherShoes : public Finery

{

public:

       virtual void Show()

       {

              printf("皮鞋 ");

              __super::Show();

       }

};

 

int main()

{

       Person* xc = new Person("小菜");

      

       printf("第一种装扮:\n");

      

       Sneakers* pqx = new Sneakers();

       BigTrouser* kk = new BigTrouser();

       TShirts* dtx = new TShirts();

      

       pqx->Decorate(xc);

       kk->Decorate(pqx);

       dtx->Decorate(kk);

       dtx->Show();

      

       printf("\n第二种装扮:\n");

      

       LeatherShoes* px = new LeatherShoes();

       Tie* ld = new Tie();

       Suit* xz = new Suit();

      

       px->Decorate(xc);

       ld->Decorate(px);

       xz->Decorate(ld);

       xz->Show();

      

       delete xc;

      

       delete pqx;

       delete kk;

       delete dtx;

      

       delete px;

       delete ld;

       delete xz;

       return 0;

}

运行结果

posted on 2011-05-05 16:04 lwch 阅读(2631) 评论(5)  编辑 收藏 引用 所属分类: 设计模式

评论:
# re: 装饰模式 2011-05-06 12:52 | 千暮(zblc)
VS10的建模工具真有美感 - -bnr   回复  更多评论
  
# re: 装饰模式 2011-05-06 16:07 | lwch
@千暮(zblc)
截图下来貌似有点变色..  回复  更多评论
  
# re: 装饰模式 2012-06-20 22:23 |
这个是装饰器模式吗??楼主请好好看看书。不要误人子弟  回复  更多评论
  
# re: 装饰模式 2012-06-21 17:35 | lwch
@林
代码来源于《大话设计模式》  回复  更多评论
  
# re: 装饰模式 2012-07-11 22:49 | liawof


#include <iostream>
#include <string>
using namespace std;

class Beverage
{
public:
Beverage():description("Unknown Beverage")
{

}
virtual std::string getDescription()//:description = "Unknown Beverage"
{
return description;
}

virtual double cost() = 0;
//{
// return 0;
//};

string description ;

protected:
private:

};


class CondimenteDecorator :public Beverage
{
public:
virtual string getDescription() = 0;
//{
// return description;
//}
protected:
private:
};


class Espresso :public Beverage
{
public:
Espresso()
{
description = "Espresso";
}


double cost()
{
return 1.99;
}
};


class HouseBlend :public Beverage
{
public:
HouseBlend()
{
description = "HouseBlend";
}


double cost()
{
return 0.99;
}
};


class DarkRoast :public Beverage
{
public:
DarkRoast()
{
description = "DarkRoast";
}


double cost()
{
return 0.88;
}
};


class Mocha :public CondimenteDecorator
{
public:
Mocha(Beverage *bevergage)
{
this->bevergage = bevergage;
}

string getDescription()
{
return bevergage->getDescription() + string(" Mocha");
}

double cost()
{
return 0.20 + bevergage->cost();
}
protected:
private:

Beverage *bevergage;
};


class Whip :public CondimenteDecorator
{
public:
Whip(Beverage *bevergage)
{
this->bevergage = bevergage;
}

string getDescription()
{
return bevergage->getDescription() + " Whip";
}

double cost()
{
return 0.40 + bevergage->cost();
}
protected:
private:

Beverage *bevergage;
};


class Soy :public CondimenteDecorator
{
public:
Soy(Beverage *bevergage)
{
this->bevergage = bevergage;
}

string getDescription()
{
return bevergage->getDescription() + " Soy";
}

double cost()
{
return 0.40 + bevergage->cost();
}
protected:
private:

Beverage *bevergage;
};




#include "Beverage.h"


int main()
{
Beverage *pBeverage = new Espresso;

std::cout<<pBeverage->getDescription().c_str()<<" $"<<pBeverage->cost()<<std::endl;


Beverage *pBeverage2 = new DarkRoast;
pBeverage2 = new Mocha(pBeverage2);
pBeverage2 = new Mocha(pBeverage2);
pBeverage2 = new Whip(pBeverage2);
std::cout<<pBeverage2->getDescription().c_str()<<" $"<<pBeverage2->cost()/**/<<std::endl;

Beverage *pBeverage3 = new HouseBlend;
pBeverage3 = new Soy(pBeverage3);
pBeverage3 = new Mocha(pBeverage3);
pBeverage3 = new Whip(pBeverage3);

std::cout<<pBeverage3->getDescription().c_str()<<" $"<<pBeverage3->cost()<<std::endl;

return 0;

}


这才是真正的装饰模式!  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理