咖啡店订单系统类设计
一:初始方案
class Bervage
{
description;
getDescription();
cost();
};
每种不同的饮料都是Bervage的一个派生类,由于调料多种多样,导致派生类“爆炸”
二 改进
class Bervage
{
description;
milk;
soy;
mocha;
whip;
getDescription();
cost();
hasMilk();
hasSoy();
hasMocha();
hasWhip();
setMilk();
setSoy();
setMocha();
setWhip();
};
基类中将所有调料作为元素,基类cost()计算添加的调料的价格
派生类中cost()中 计算不同的饮料的价格加上基类cost计算出来的调料的价格,作为某个单品的总价格
存在的问题:
1)调料价格的改变需要更新现有代码
2)一旦出现新的调料,需要改变基类代码
3)某些单品与调料并不搭配,这些单品的类中依然存在调料
4)双倍或多倍调料问题
三 装饰者模式
装饰者模式将以饮料为主体,在运行时以调料来装饰饮料。例如如果顾客要摩卡和奶茶深焙咖啡,需要做的是:
1)一个深焙咖啡对象
2)以摩卡对象装饰它
3)以奶茶对象装饰它
4)调用cost方法,并依赖委托将调料的价格加上去
以下转自:http://www.jellythink.com/archives/171
前言
类封装了一个对象的核心操作,而这些操作就是客户使用该类时都会去调用的操作
有一些非核心的操作,可能会使用,也可能不会使用;现在该怎么办呢?
1)将非核心的操作全部放到类中,这样,一个类就包含了很多核心的操作和一些看似有关,但是又无关的操作;
这就会使核心类发生“爆炸”的现象,从而使核心类失去了一定的价值,也使使用核心类的客户在核心操作和非核心操作中挣扎;
2)使用继承来扩展核心类,需要使用核心类时,直接建立核心类对象;
当需要使用核心类扩展类时,就建立核心类扩展类对象;这样貌似是一种很有效的方法
但是由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性,同时随着扩展功能的增多,子类也会增多,各种子类的组合,就会导致类的膨胀
什么是装饰模式
装饰模式能够实现从对象的外部,动态的给对象添加功能 。
通常给对象添加功能,要么直接修改对象添加相应的功能,要么通过派生类来扩展。
在面向对象的设计中,应尽量使用对象组合,而不是对象继承来扩展和复用功能。
装饰者模式就是通过对象组合的方式,灵活的给对象添加所需要的功能。
装饰模式是通过把复杂的功能简单化,分散化,然后在运行期间,根据需要来动态组合的这样一个模式。它使得我们可以给某个对象而不是整个类添加一些功能
Component:定义对象接口,可以给这些对象动态地添加职责(对象基类)
ConcreteComponent:定义一个具体的Component,继承自Component,重写了Component类的虚函数(对象派生类)
Decorator:维持一个指向Component对象的指针,该指针指向需要被装饰的对象
同时定义一个与Component接口一致的接口,该接口将调用其维护的指针的operation操作
ConcreteDecorator:向组件添加职责,该类的operation操作将先执行添加操作,再执行Decorator::operation()
对于ConcreteDecotator,可以继续添加装饰
/*
** FileName : DecoratorPatternDemo
** Author : Jelly Young
** Date : 2013/12/19
** Description : More information, please go to http://www.jellythink.com
*/
#include <iostream>
using namespace std;
class Component
{
public:
virtual void Operation() = 0;
};
class ConcreteComponent : public Component
{
public:
void Operation()
{
cout<<"I am no decoratored ConcreteComponent"<<endl;
}
};
class Decorator : public Component
{
public:
Decorator(Component *pComponent) : m_pComponentObj(pComponent) {}
void Operation()
{
if (m_pComponentObj != NULL)
{
m_pComponentObj->Operation();
}
}
protected:
Component *m_pComponentObj;
};
class ConcreteDecoratorA : public Decorator
{
public:
ConcreteDecoratorA(Component *pDecorator) : Decorator(pDecorator){}
void Operation()
{
AddedBehavior();
Decorator::Operation();
}
void AddedBehavior()
{
cout<<"This is added behavior A."<<endl;
}
};
class ConcreteDecoratorB : public Decorator
{
public:
ConcreteDecoratorB(Component *pDecorator) : Decorator(pDecorator){}
void Operation()
{
AddedBehavior();
Decorator::Operation();
}
void AddedBehavior()
{
cout<<"This is added behavior B."<<endl;
}
};
int main()
{
Component *pComponentObj = new ConcreteComponent();
Decorator *pDecoratorAOjb = new ConcreteDecoratorA(pComponentObj);
pDecoratorAOjb->Operation();
cout<<"============================================="<<endl;
Decorator *pDecoratorBOjb = new ConcreteDecoratorB(pComponentObj);
pDecoratorBOjb->Operation();
cout<<"============================================="<<endl;
Decorator *pDecoratorBAOjb = new ConcreteDecoratorB(pDecoratorAOjb);
pDecoratorBAOjb->Operation();
cout<<"============================================="<<endl;
delete pDecoratorBAOjb;
pDecoratorBAOjb = NULL;
delete pDecoratorBOjb;
pDecoratorBOjb = NULL;
delete pDecoratorAOjb;
pDecoratorAOjb = NULL;
delete pComponentObj;
pComponentObj = NULL;
}