当前位置:网站首页>06 decorator mode
06 decorator mode
2022-07-02 04:47:00 【zzyzxb】
One : Problem presentation

Wrapped by the protagonist of the game UI Interface 
Announcement information appearing in the game UI Interface
UI( The user interface ) Interface .
a)ListCtrl Class represents a common list control , Provide draw Method .
b)BorderListCtrl class , Inherited from ListCtrl, List control with border added , Provide draw Method .
c)VerScBorderListCtrl Class inherits from BorderListCtrl, Indicates a list control that adds a border and a vertical scroll bar , Provide draw Method .
d)HorScVerScBorderListCtrl class , Inherited from VerScBorderListCtrl, Indicates that the border is added , vertical 、 List control of horizontal scroll bar , Provide draw Method .

Successively add borders for a common list control 、 Vertical scroll bar 、 Effect after horizontal scroll bar
Change inheritance to assembly , Prevent class flooding
a)ListCtrl Class represents a common list control , Provide draw Method .
b) Add border -> List control with border .
c) Add vertical scroll bar -> List control with pure scroll bar , Add a horizontal scroll bar to the list control with a vertical scroll bar -> List control with both vertical and horizontal scroll bars .

Adding different decorations to list controls can get different list controls
This idea of constantly enhancing the function of a class through decoration ( Dynamically add new functions ), It is the core design idea of decoration mode .
public Inherit :is -a Relationship , Composition and delegation .
Two : Introduction decoration (Decorator) Pattern
Principle of combined reuse (Composite Reuse Principle,CRP), Also known as the composite Reuse Principle / Principle of aggregation and reuse .
If two use inheritance to design , Then the modification of the parent class code may affect the behavior of the child class , and , Maybe many methods in the parent class cannot be used by subclasses , It's obviously a waste ,
If you use combination to design , Can greatly reduce the dependency between the two classes , There will be no wasteful behavior caused by inheritance . therefore , If inheritance and combination can achieve the design purpose , Give priority to combinations ( Combination over inheritance ).
#ifdef _DEBUG // Only in Debug( debugging ) In mode
#ifndef DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) // Redefinition new Operator
#define new DEBUG_NEW
#endif
#endif
//#include <boost/type_index.hpp>
using namespace std;
//#pragma warning(disable : 4996)
namespace nmsp1
{
// Abstract control class
class Control
{
public:
virtual void draw() = 0; //draw Method , Used to draw itself onto the screen .
public:
virtual ~Control() {} // As a parent class, the destructor should be a virtual function
};
// List control class
class ListCtrl :public Control
{
public:
virtual void draw()
{
cout << " Draw ordinary list controls !" << endl; // You can use DirectX or OpenGL To draw
}
};
// Abstract decorator class
class Decorator :public Control
{
public:
Decorator(Control* tmpctrl) :m_control(tmpctrl) {} // Constructors
virtual void draw()
{
m_control->draw(); // Virtual functions , Which is called draw, Depending on m_control Object to point to
}
private:
Control* m_control; // Other controls that need to be decorated , It's used here Control*;
};
// Concrete “ Frame ” Decorator class
class BorderDec :public Decorator
{
public:
BorderDec(Control* tmpctrl) :Decorator(tmpctrl) {} // Constructors
virtual void draw()
{
Decorator::draw(); // Calling the draw Method to maintain what has been drawn in the past
drawBorder(); // Also draw your own content
}
private:
void drawBorder()
{
cout << " bound box !" << endl;
}
};
// Concrete “ Vertical scroll bar ” Decorator class
class VerScrollBarDec :public Decorator
{
public:
VerScrollBarDec(Control* tmpctrl) :Decorator(tmpctrl) {} // Constructors
virtual void draw()
{
Decorator::draw(); // Calling the draw Method to maintain what has been drawn in the past
drawVerScrollBar(); // Also draw your own content
}
private:
void drawVerScrollBar()
{
cout << " Draw a vertical scroll bar !" << endl;
}
};
// Concrete “ Horizontal scroll bar ” Decorator class
class HorScrollBarDec :public Decorator
{
public:
HorScrollBarDec(Control* tmpctrl) :Decorator(tmpctrl) {} // Constructors
virtual void draw()
{
Decorator::draw(); // Calling the draw Method to maintain what has been drawn in the past
drawHorScrollBar(); // Also draw your own content
}
private:
void drawHorScrollBar()
{
cout << " Draw a horizontal scroll bar !" << endl;
}
};
}
int main()
{
//(1) Create one with borders , List control with vertical scroll bar
// First draw a common list control
nmsp1::Control* plistctrl1 = new nmsp1::ListCtrl(); // noumenon
// next “ With the help of ordinary list controls ”, You can draw a “ List control with border ”
nmsp1::Decorator* plistctrl_b = new nmsp1::BorderDec(plistctrl1); //nmsp1::Decorator* Used as nmsp1::Control*
// next “ With the help of list controls with borders ”, You can use the vertical scroll bar decorator to draw a “ List control with vertical scroll bar and border ”
nmsp1::Decorator* plistctrl_b_v = new nmsp1::VerScrollBarDec(plistctrl_b);
plistctrl_b_v->draw(); // Here is the final drawing
cout << "--------------------" << endl;
//(2) Create a list control with only horizontal scroll bars
// First draw a common list control
nmsp1::Control* plistctrl2 = new nmsp1::ListCtrl(); // noumenon
// next “ With the help of ordinary list controls ”, You can draw a through the horizontal scroll bar decorator “ List control with horizontal scroll bar ”
nmsp1::Decorator* plistctrl2_h = new nmsp1::HorScrollBarDec(plistctrl2);
plistctrl2_h->draw();
//(3) Release resources
delete plistctrl_b_v;
delete plistctrl_b;
delete plistctrl1;
delete plistctrl2_h;
delete plistctrl2;
std::cout << " Main thread execution completed \n";
}

Decoration mode UML chart
“ decorate ” Definition of design pattern ( Realize the intention ): Dynamically add some extra responsibilities to an object . In terms of adding functionality , This pattern is more flexible than generating subclasses .
Decoration mode contains four roles :
a:Control( Abstract component ):draw, Let the caller deal with unmodified objects and modified objects in a consistent way , Realize the transparent operation of the client .
b:ListCtrl( The specific components ): Implement the interface defined by the abstract component , thereafter , The decorator can add additional methods to the component ( duty ).
c:Decorator( Abstract decorator class ).
d:BorderDec、VerScrollBarDec、HorScrollBarDesc( Specific decorators ): Added some new methods , And then through the right draw Interface extension , Achieve the ultimate purpose of decoration .
3、 ... and : Another example of decorative patterns
Calculate the final price of fruit drinks
a) A simple fruit drink , The price is 10 element .
b) If sugar is added to the drink , Then add more 1 element .
c) If milk is added to the drink , Then add more 2 element .
d) If you add pearls to your drink , Then add more 2 element .
e) Add pearls and sugar ,10+2+1 = 13.
namespace nmsp2
{
// Abstract beverage class
class Beverage
{
public:
virtual int getprice() = 0; // Get price
public:
virtual ~Beverage() {}
};
// Fruit drinks
class FruitBeverage :public Beverage
{
public:
virtual int getprice()
{
return 10; // A simple fruit drink , The price is 10 element .
}
};
// Abstract decorator class
class Decorator :public Beverage
{
public:
Decorator(Beverage* tmpbvg) :m_pbvg(tmpbvg) {} // Constructors
virtual int getprice()
{
return m_pbvg->getprice();
}
private:
Beverage* m_pbvg;
};
// Concrete “ Sugar ” Decorator class
class SugarDec :public Decorator
{
public:
SugarDec(Beverage* tmpbvg) :Decorator(tmpbvg) {} // Constructors
virtual int getprice()
{
return Decorator::getprice() + 1; // Extra extra 1 element , To call the getprice Method to increase the previous price
}
};
// Concrete “ milk ” Decorator class
class MilkDec :public Decorator
{
public:
MilkDec(Beverage* tmpbvg) :Decorator(tmpbvg) {} // Constructors
virtual int getprice()
{
return Decorator::getprice() + 2; // Extra extra 2 element , To call the getprice Method to increase the previous price
}
};
// Concrete “ Pearl ” Decorator class
class BubbleDec :public Decorator
{
public:
BubbleDec(Beverage* tmpbvg) :Decorator(tmpbvg) {} // Constructors
virtual int getprice()
{
return Decorator::getprice() + 2; // Extra extra 2 element , To call the getprice Method to increase the previous price
}
};
}
int main()
{
// Create a simple fruit drink , Price 10 element :
nmsp2::Beverage* pfruit = new nmsp2::FruitBeverage();
// Add pearls to the drink , The price is increased 2 element
nmsp2::Decorator* pfruit_addbubb = new nmsp2::BubbleDec(pfruit);
// Add sugar to the drink , The price has increased again 1 element
nmsp2::Decorator* pfruit_addbubb_addsugar = new nmsp2::SugarDec(pfruit_addbubb);
// Output the final price
cout << " The final price of fruit drinks with pearls and sugar is :" << pfruit_addbubb_addsugar->getprice() << " RMB " << endl;
// Release resources
delete pfruit_addbubb_addsugar;
delete pfruit_addbubb;
delete pfruit;
std::cout << " Main thread execution completed \n";
}

The decorative mode of milk tea and fruit drinks UML chart
边栏推荐
- 缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
- What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
- Introduction to Luogu 3 [circular structure] problem list solution
- 千亿市场规模医疗美容行业的水究竟有多浑?
- geotrust ov多域名ssl证书一年两千一百元包含几个域名?
- Deep understanding of lambda expressions
- Binary tree problem solving (1)
- CY7C68013A之keil编译代码
- 农业生态领域智能机器人的应用
- Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
猜你喜欢

Cannot activate CONDA virtual environment in vscode

Steam教育的实际问题解决能力

Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification

DC-1靶场搭建及渗透实战详细过程(DC靶场系列)

June book news | 9 new books are listed, with a strong lineup and eyes closed!

Change deepin to Alibaba image source

万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!

关于Steam 教育的知识整理

idea自动导包和自动删包设置

cs架构下抓包的几种方法
随机推荐
VMware installation win10 reports an error: operating system not found
MySQL table insert Chinese change? Solution to the problem of No
Embedded-c language-9-makefile/ structure / Consortium
Summary of main account information of zhengdaliu 4
Homework of the 16th week
解析少儿编程中的动手搭建教程
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
Unit testing classic three questions: what, why, and how?
洛谷入门3【循环结构】题单题解
The solution to the complexity brought by lambda expression
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Change deepin to Alibaba image source
Markdown编辑语法
One click generation and conversion of markdown directory to word format
C language practice - number guessing game
Deep understanding of lambda expressions
unable to execute xxx. SH: operation not permitted
2022-003arts: recursive routine of binary tree
初学爬虫-笔趣阁爬虫
千亿市场规模医疗美容行业的水究竟有多浑?