认识C++设计模式组合模式

组合模式(Composite Pattern),用于表示对象的部分-整体层次结构。
在组合模式中,可以将对象组合成树形结构,使得客户端对单个对象和组合对象的使用具有一致性。

组合模式通常包括以下角色:

    Component(组件):这是组合中的基类或接口,定义了所有子类共有的接口。它既可以代表叶子对象,也可以代表容器对象,也就是组合对象。

    Leaf(叶子):叶子对象,在组合中表示单个对象,没有子节点。它实现了 Component 接口或继承了 Component 类。

    Composite(容器/组合):容器对象,含有子部件的组件,实现了 Component 接口或继承了 Component 类。它存储子部件,并在 Component 接口中实现与子部件相关的操作。

一个简单示例;

#include 
#include 
#include 
#include 
 
// Component 接口
class Graphic {
public:
    virtual void Print() cons

你可能感兴趣的:(VC++,c++,设计模式,组合模式)