当前位置:网站首页>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
边栏推荐
- win10 磁盘管理 压缩卷 无法启动问题
- Mysql表insert中文变?号的问题解决办法
- I sorted out some basic questions about opencv AI kit.
- TypeScript函数详解
- 二叉树解题(二)
- Pytoch --- use pytoch to predict birds
- Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
- Mysql database learning
- [improvement class] st table to solve the interval maximum value problem [2]
- Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
猜你喜欢

Learn what definitelytyped is through the typescript development environment of SAP ui5

Several methods of capturing packets under CS framework

Pytoch --- use pytoch to predict birds

Solution of DM database unable to open graphical interface

阿里云polkit pkexec 本地提权漏洞

Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知

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

The solution to the complexity brought by lambda expression

Idea automatic package import and automatic package deletion settings

Federal learning: dividing non IID samples according to Dirichlet distribution
随机推荐
Landing guide for "prohibit using select * as query field list"
List of common bugs in software testing
win11安装pytorch-gpu遇到的坑
Binary tree problem solving (2)
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
Common locks in MySQL
[Yu Yue education] autumn 2021 reference materials of Tongji University
解决:代理抛出异常错误
TypeScript函数详解
【ClickHouse】How to create index for Map Type Column or one key of it?
There is no prompt for SQL in idea XML, and the dialect setting is useless.
idea自動導包和自動删包設置
Markdown edit syntax
DJB Hash
Pytoch yolov5 runs bug solution from 0:
Typescript function details
[C language] basic learning notes
Markdown编辑语法
Beginner crawler - biqu Pavilion crawler
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial