当前位置:网站首页>Composite mode
Composite mode
2022-06-26 13:11:00 【baboon_ chen】
Reference resources :
Composite design patterns (refactoringguru.cn)
Portfolio model ( Detailed Edition ) (biancheng.net)
design-patterns-cpp/composite at master · JakubVojvoda/design-patterns-cpp · GitHub
One 、 What is a combination pattern ?
Definition : Combine objects into a tree hierarchy , So that the user can understand
Leaf nodes( Single object ) andTree node( Combine objects ) Have consistent accessibility .
The most important point is , The objects that users need to process can be represented in a hierarchical structure . The tree itself is a recursive hierarchical structure . If both tree nodes and leaf nodes implement the same interface , So no matter how the tree is expanded or reduced , Will not affect its access .
For example, the armies of most countries adopt hierarchical management . Each unit consists of several divisions , A division consists of brigades , Brigade consists of regiments , The regiment can continue to be divided into rows . Last , Each platoon consists of a small group of real soldiers . Military orders are issued at the highest level , Pass... Through each level , Until every soldier knows what he should obey . No matter how variable the number of troops , Orders are always given .


Two 、 Realization
Combine (Composite ) The pattern includes the following main characters :
- Abstract component (Component) role : Its main function is to declare the common interface for the leaf component and the branch component , And implement their default behavior . Abstract components also declare interfaces to access and manage subclasses in transparent composition patterns ; Interfaces to access and manage subclasses are not declared in a secure composite pattern , The management work is done by the branch component .( Total abstract class or interface , Define some general methods , For example, adding 、 Delete )
- Leaf components (Leaf) role : Is the leaf node object in the assembly , It has no child nodes , Used to inherit or implement abstract components .
- Tree branches (Composite) role / Intermediate members : Is the branch node object in the composite , It has children , Used to inherit and implement abstract components . Its main function is to store and manage sub components , Usually contains Add()、Remove()、GetChild() Other methods .
The combination mode can be divided into transparent combination mode and safe combination mode .
1、 Transparent way
Leaf nodes (Leaf) And Tree node (Composite) It's all done Abstract component (Component) Interface . If Component It defines Add()、Remove()、GetChild() Other methods , Leaf nodes don't need , But to achieve them ( Empty implementation or throw exception ), This will bring some security problems . This has the advantage that the client does not have to distinguish between leaf objects and branch objects , Transparent to clients .

/*
* C++ Design Patterns: Composite
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
* 2016
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*/
#include <iostream>
#include <vector>
/*
* Component
* Declare common interfaces for leaf and branch components
* Here is the transparent combination mode , Abstract artifacts also declare interfaces to access and manage subclasses :Add\remove\getChild
*/
class Component
{
public:
virtual ~Component() {}
virtual Component *getChild( int )
{
return 0;
}
virtual void add( Component * ) { /* ... */ }
virtual void remove( int ) { /* ... */ }
virtual void operation() = 0;
};
/*
* Composite
* Is the tree node object in the composition , It has children , Used to inherit and implement abstract components .
* Its main function is to store and manage sub components
*/
class Composite : public Component
{
public:
~Composite()
{
for ( unsigned int i = 0; i < children.size(); i++ )
{
delete children[ i ];
}
}
Component *getChild( const unsigned int index )
{
return children[ index ];
}
void add( Component *component )
{
children.push_back( component );
}
void remove( const unsigned int index )
{
Component *child = children[ index ];
children.erase( children.begin() + index );
delete child;
}
void operation()
{
for ( unsigned int i = 0; i < children.size(); i++ )
{
children[ i ]->operation();
}
}
private:
std::vector<Component*> children;
};
/*
* Leaf
* The leaf node object in the combination , It has no child nodes , Used to inherit or implement abstract components
*/
class Leaf : public Component
{
public:
Leaf( const int i ) : id( i ) {}
~Leaf() {}
void operation()
{
std::cout << "Leaf "<< id <<" operation" << std::endl;
}
private:
int id;
};
int main()
{
Composite composite;
for ( unsigned int i = 0; i < 5; i++ )
{
composite.add( new Leaf( i ) );
}
composite.remove( 0 );
composite.operation();
return 0;
}
2、 safe mode
The method of managing leaf nodes is implemented only in tree nodes (Add、Remove etc. ).

#include <iostream>
#include <vector>
class Component
{
public:
virtual ~Component() {}
virtual void operation() = 0;
};
class Composite : public Component
{
public:
~Composite()
{
for ( unsigned int i = 0; i < children.size(); i++ )
{
delete children[ i ];
}
}
Component *getChild( const unsigned int index )
{
return children[ index ];
}
void add( Component *component )
{
children.push_back( component );
}
void remove( const unsigned int index )
{
Component *child = children[ index ];
children.erase( children.begin() + index );
delete child;
}
void operation()
{
for ( unsigned int i = 0; i < children.size(); i++ )
{
children[ i ]->operation();
}
}
private:
std::vector<Component*> children;
};
class Leaf : public Component
{
public:
Leaf( const int i ) : id( i ) {}
~Leaf() {}
void operation()
{
std::cout << "Leaf "<< id <<" operation" << std::endl;
}
private:
int id;
};
int main()
{
Composite composite;
for ( unsigned int i = 0; i < 5; i++ )
{
composite.add( new Leaf( i ) );
}
composite.remove( 0 );
composite.operation();
return 0;
}
3、 ... and 、 Advantages and disadvantages , Applicable scenario
advantage
- The composite pattern allows client code to consistently handle individual and composite objects .
- It's easier to add new objects to the mix , The client will not change the source code because of adding new objects , It satisfies the open close principle .
shortcoming
- The design is more complicated , The client needs to spend more time clarifying the hierarchical relationship between classes .
- For classes with different functions , It may be difficult to provide a common interface .
边栏推荐
- PostGIS calculation angle
- scrapy——爬取漫画自定义存储路径下载到本地
- 软件测试测试常见分类有哪些?
- HDU 3709 Balanced Number
- Record a phpcms9.6.3 vulnerability to use the getshell to the intranet domain control
- 倍福EtherCAT Xml描述文件更新和下载
- Basic principle and application routine of Beifu PLC rotary cutting
- 心脏滴血漏洞(CVE-2014-0160)分析与防护
- Detailed explanation of C const: definition and use of C constant
- Processsing 鼠标交互 学习
猜你喜欢

P2393 yyy loves Maths II

Don't mess with full_ Case and parallel_ CASE
Summary of wechat applet test points

倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换

First pass! Baidu AI Cloud Xiling platform has obtained the authoritative certification of digital human ability evaluation from the Institute of information technology

scrapy——爬取漫画自定义存储路径下载到本地

适配器模式(Adapter)

Processing function translate (mousex, mousey) learning

Design of four kinds of linear phase FIR filters -- complete set of Matlab source code

Enjoy element mode (flyweight)
随机推荐
Go 结构体方法
Dark horse notes - Common APIs
倍福PLC基于NT_Shutdown实现控制器自动关机重启
倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络
利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)
F - Charm Bracelet
首批通过!百度智能云曦灵平台获信通院数字人能力评测权威认证
Electron official docs series: Contributing
HDU 5860
P5733 【深基6.例1】自动修正
Basic principle and application routine of Beifu PLC rotary cutting
Electron official docs series: References
Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
复制多个excel然后命名不同的名字
D - 滑雪
PostGIS geographic function
J - Wooden Sticks poj 1065
P5733 [deep foundation 6. example 1] automatic correction
Uva10341 solve it
Processsing mouse interactive learning