• 【20】c++设计模式——>组合模式


    组合模式定义

    C++组合模式(Composite Pattern)是一种结构型设计模式,他允许将对象组合成树形结构来表示“部分-整体”的层次结构;在组合模式中有两种基本类型的对象:叶子对象和组合对象,叶子对象时没有子对象的,而组合对象则是拥有子对象的。叶子对象和组合对象都实现了相同的接口,以便可以用相同的方式处理他们。

    简单的C++组合模式实例

    #include 
    #include
    
    //抽象基类,定义组合对象和叶子对象的公共接口,
    class Component
    {
    public:
    	//纯虚函数,需要子类实现,
    	virtual void operation() = 0;
    };
    
    //叶子对象,无子对象,实现Component接口
    class Leaf :public Component
    {
    public:
    	void operation() override
    	{
    		std::cout << "Lead operation" << std::endl;
    	}
    };
    //组合对象,拥有子对象,实现Component接口
    class Composite :public Component
    {
    public:
    	//添加子对象到子对象列表中
    	void add(Component* component)
    	{
    		children.push_back(component);
    	}
    	void operation() override
    	{
    		std::cout << "Composite operation" << std::endl;
    		for (auto child : children)
    		{
    			child->operation();
    		}
    	}
    private:
    	std::vector<Component*>children; //子对象列表
    };
    int main()
    {
    	Leaf leaf;
    	 
    	Composite composite;
    	composite.add(&leaf);
    	composite.add(&leaf);
    	composite.add(&leaf);
    
    	composite.operation();
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在这里插入图片描述

    文件系统来演示组合模式

    我们以一个文件系统为例,使用组合模式来表示文件系统中的目录文件的关系

    #include 
    #include
    #include
    
    //抽象基类,定义了组合对象和叶子对象的公共接口
    class FileSystemComponent
    {
    public:
    	virtual void showInfo() = 0;
    	virtual ~FileSystemComponent() {};
    };
    //叶子对象:文件
    class File :public FileSystemComponent
    {
    public:
    	File(const std::string& name) :m_Name(name) {}
    	void showInfo() override
    	{
    		std::cout << "File:" << m_Name << std::endl;
    	}
    private:
    	std::string m_Name;
    };
    
    //组合对象:目录
    class Directory :public FileSystemComponent
    {
    public:
    	Directory(const std::string& name) :m_Name(name) {}
    	void add(FileSystemComponent* com)
    	{
    		children.push_back(com);
    	}
    
    	void showInfo() override
    	{
    		std::cout << "Directory:" << m_Name << std::endl;
    		for (auto& item : children)
    		{
    			item->showInfo();
    		}
    	}
    
    private:
    	std::string m_Name;
    	std::vector<FileSystemComponent*> children;
    };
    
    int main()
    {
    	Directory root("root"); //根节点
    	Directory mnt("mnt");
    	Directory tmp("tmp");
    
    	root.add(&mnt);
    	root.add(&tmp);
    
    	File ext("ext");
    	File nfs("nfs");
    
    	mnt.add(&ext);
    	tmp.add(&nfs);
    
    	root.showInfo();
    
    	return 0;
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    在这里插入图片描述

  • 相关阅读:
    el-input: 把不符合正则校验的值动态清空,只保留符合的值
    贝塞尔函数
    鸿蒙OS开发:【一次开发,多端部署】(app市场首页)项目
    react,Chart
    【ldt_struct】0ctf2021-kernote
    IDEA 数据库插件Database Navigator 插件
    技术人员如何有效进行各种职场排挤、防止被排挤?
    思科华为设备端口聚合配置命令对比
    【PD】—review
    RT-Thread 项目工程搭建和配置--(Env Kconfig)
  • 原文地址:https://blog.csdn.net/weixin_42097108/article/details/133692625