当前位置:网站首页>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
边栏推荐
- Embedded-c language-9-makefile/ structure / Consortium
- 解析少儿编程中的动手搭建教程
- Research on the security of ognl and El expressions and memory horse
- Several methods of capturing packets under CS framework
- cs架构下抓包的几种方法
- List of common bugs in software testing
- [understand one article] FD_ Use of set
- I sorted out some basic questions about opencv AI kit.
- 记录一次Unity 2020.3.31f1的bug
- Learn BeanShell before you dare to say you know JMeter
猜你喜欢
Let正版短信测压开源源码
DMA Porter
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Rhcsa --- work on the fourth day
Landing guide for "prohibit using select * as query field list"
LeetCode-对链表进行插入排序
Unity particle Foundation
Mysql表insert中文变?号的问题解决办法
LeetCode-归并排序链表
Future trend of automated testing ----- self healing technology
随机推荐
GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
Binary tree problem solving (2)
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
Markdown编辑语法
Learn BeanShell before you dare to say you know JMeter
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Leetcode merge sort linked list
Pytorch-Yolov5从0运行Bug解决:
Oracle stored procedure and job task setting
idea自动导包和自动删包设置
解决:代理抛出异常错误
Beginner crawler - biqu Pavilion crawler
Read "the way to clean code" - function names should express their behavior
Mysql database learning
C language practice - binary search (half search)
Use of Baidu map
Pytest learning ----- pytest assertion of interface automation testing
DMA Porter
奠定少儿编程成为基础学科的原理