当前位置:网站首页>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
边栏推荐
- Common errors of dmrman offline backup
- Typescript function details
- Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
- Markdown edit syntax
- Mysql表insert中文变?号的问题解决办法
- CY7C68013A之keil编译代码
- 农业生态领域智能机器人的应用
- 二叉树解题(二)
- Solution of DM database unable to open graphical interface
- Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
猜你喜欢
![[understand one article] FD_ Use of set](/img/57/276f5ef438adee2cba31dceeabb95c.jpg)
[understand one article] FD_ Use of set

List of common bugs in software testing

Learn BeanShell before you dare to say you know JMeter

Future trend of automated testing ----- self healing technology

10 minute quick start UI automation ----- puppeter

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

Idea automatic package import and automatic package deletion settings

Unity particle Foundation

unable to execute xxx. SH: operation not permitted

Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
随机推荐
Promise all()
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
CorelDRAW Graphics Suite2022免费图形设计软件
Embedded-c language-9-makefile/ structure / Consortium
Binary tree problem solving (1)
[understand one article] FD_ Use of set
Pytorch-Yolov5從0運行Bug解决:
Orthogonal test method and function diagram method for test case design
社交媒体搜索引擎优化及其重要性
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
千亿市场规模医疗美容行业的水究竟有多浑?
Comp 250 parsing
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
What data does the main account of Zhengda Meiou 4 pay attention to?
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Win10 disk management compressed volume cannot be started
DJB Hash
Pytoch --- use pytoch to realize u-net semantic segmentation