《C++新经典设计模式》之第6章 装饰器模式
发布时间:2023年12月17日
装饰器模式.cpp
#include <iostream>
#include <memory>
using namespace std;
namespace ns1
{
class Control
{
public:
virtual ~Control() {}
public:
virtual void draw() const = 0;
};
class ListCtrl : public Control
{
public:
void draw() const override { cout << "Draw Common List Controls!" << endl; }
};
class Decorator : public Control
{
shared_ptr<Control> m_control;
public:
Decorator(const shared_ptr<Control> &tmpctrl) : m_control(tmpctrl) {}
void draw() const override { m_control->draw(); }
};
class BorderDec : public Decorator
{
void drawBorder() const { cout << "bound box!" << endl; }
public:
BorderDec(const shared_ptr<Control> &tmpctrl) : Decorator(tmpctrl) {}
void draw() const override
{
Decorator::draw();
drawBorder();
}
};
class VerScrollBarDec : public Decorator
{
void drawVerScrollBar() const { cout << "Draw vertical scroll bar!" << endl; }
public:
VerScrollBarDec(const shared_ptr<Control> &tmpctrl) : Decorator(tmpctrl) {}
void draw() const override
{
Decorator::draw();
drawVerScrollBar();
}
};
class HorScrollBarDec : public Decorator
{
void drawHorScrollBar() const { cout << "Draw horizontal scroll bar!" << endl; }
public:
HorScrollBarDec(const shared_ptr<Control> &tmpctrl) : Decorator(tmpctrl) {}
void draw() const override
{
Decorator::draw();
drawHorScrollBar();
}
};
}
namespace ns2
{
class Beverage
{
public:
virtual ~Beverage() {}
virtual int getprice() const = 0;
};
class FruitBeverage : public Beverage
{
public:
int getprice() const override { return 10; }
};
class Decorator : public Beverage
{
shared_ptr<Beverage> m_pbvg;
public:
Decorator(const shared_ptr<Beverage> &tmpbvg) : m_pbvg(tmpbvg) {}
int getprice() const override { return m_pbvg->getprice(); }
};
class SugarDec : public Decorator
{
public:
SugarDec(const shared_ptr<Beverage> &tmpbvg) : Decorator(tmpbvg) {}
int getprice() const override { return Decorator::getprice() + 1; }
};
class MilkDec : public Decorator
{
public:
MilkDec(const shared_ptr<Beverage> &tmpbvg) : Decorator(tmpbvg) {}
int getprice() const override { return Decorator::getprice() + 2; }
};
class BubbleDec : public Decorator
{
public:
BubbleDec(const shared_ptr<Beverage> &tmpbvg) : Decorator(tmpbvg) {}
int getprice() const override { return Decorator::getprice() + 2; }
};
}
namespace ns3
{
class Shape
{
public:
virtual ~Shape() = default;
virtual void draw() const = 0;
};
class Rectangle : public Shape
{
public:
void draw() const override { cout << "Shape: Rectangle" << endl; }
};
class Circle : public Shape
{
public:
void draw() const override { cout << "Shape: Circle" << endl; }
};
class ShapeDecorator : public Shape
{
protected:
shared_ptr<Shape> decoratedShape;
public:
ShapeDecorator(const shared_ptr<Shape> &s) : decoratedShape(s) {}
void draw() const override { decoratedShape->draw(); }
};
class RedShapeDecorator : public ShapeDecorator
{
void setRedBorder() const { cout << "Border Color: Red" << endl; }
public:
RedShapeDecorator(const shared_ptr<Shape> &s) : ShapeDecorator(s) {}
void draw() const override
{
decoratedShape->draw();
setRedBorder();
}
};
}
int main()
{
#if 0
using namespace ns1;
shared_ptr<Control> plistctrl(new ListCtrl());
shared_ptr<Control> plistctrl_b(new BorderDec(plistctrl));
shared_ptr<Control> plistctrl_b_v(new VerScrollBarDec(plistctrl_b));
plistctrl_b_v->draw();
cout << "-------------------------------" << endl;
shared_ptr<Control> plistctrl2(new ListCtrl());
shared_ptr<Control> plistctrl2_h(new HorScrollBarDec(plistctrl2));
plistctrl2_h->draw();
#endif
#if 0
using namespace ns2;
shared_ptr<Beverage> pfruit(new FruitBeverage());
shared_ptr<Beverage> pfruit_addbubb(new BubbleDec(pfruit));
shared_ptr<Beverage> pfruit_addbubb_addsugar(new SugarDec(pfruit_addbubb));
cout << "last price: " << pfruit_addbubb_addsugar->getprice() << endl;
#endif
#if 1
using namespace ns3;
shared_ptr<Shape> circle(new Circle());
cout << "Circle with normal border" << endl;
circle->draw();
shared_ptr<Shape> redCircle(new RedShapeDecorator(circle));
cout << "\nCircle of red border" << endl;
redCircle->draw();
shared_ptr<Shape> rectange(new Rectangle());
shared_ptr<Shape> redRectangle(new RedShapeDecorator(rectange));
cout << "\nRectangle of red border" << endl;
redRectangle->draw();
#endif
cout << "Over!\n";
return 0;
}
文章来源:https://blog.csdn.net/oqqyx1234567/article/details/128665708
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!