当前位置:网站首页>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
边栏推荐
- Realize the function of data uploading
- GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
- 缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
- 2022-003arts: recursive routine of binary tree
- What data does the main account of Zhengda Meiou 4 pay attention to?
- [C language] Dynamic Planning --- from entry to standing up
- Pytorch-Yolov5從0運行Bug解决:
- Online incremental migration of DM database
- Free drawing software recommended - draw io
- 阿里云polkit pkexec 本地提权漏洞
猜你喜欢
Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
Leetcode- insert and sort the linked list
培养中小学生对教育机器人的热爱之心
Online incremental migration of DM database
Several methods of capturing packets under CS framework
CorelDRAW graphics suite2022 free graphic design software
Record the bug of unity 2020.3.31f1 once
洛谷入门3【循环结构】题单题解
Federal learning: dividing non IID samples according to Dirichlet distribution
Virtual machine installation deepin system
随机推荐
初学爬虫-笔趣阁爬虫
Idea autoguide package and autodelete package Settings
Ruby replaces gem Alibaba image
记录一次Unity 2020.3.31f1的bug
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
【毕业季·进击的技术er】年少有梦,何惧彷徨
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
二叉樹解題(二)
How to write a client-side technical solution
正大留4的主账户信息汇总
Major domestic quantitative trading platforms
Hcip day 17
There is no prompt for SQL in idea XML, and the dialect setting is useless.
What methods should service define?
Free drawing software recommended - draw io
Detailed process of DC-1 range construction and penetration practice (DC range Series)
[understand one article] FD_ Use of set
Why can't you remember when reading? Why can't you remember- My technology learning methodology