• 设计模式C++学习笔记之十五(Composite组合模式)


     

    15.1.解释

    概念:将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合的使用具有一致性。

    main(),客户

    CCorpNode,抽象基类,实现基本信息

    CBranchNode,树枝节点,实现Addordinate()函数和GetSubordinate()函数

    CLeafNode,叶子节点,IsLeaf属性总是“true”

    说明:组合模式主要是实现在CBranchNode对象里增加对其它对象的数组,如vector<CCorpNode*>,数组里可以存放CBranchNode和CLeafNode对象。这样方便进行遍历操作。

    注意:组合模式有透明组合模式和安全组合模式。透明组合模式是将Addordinate和GetSubordinate这两个函数也抽象到CCorpNode基类里,这增加了操作叶子节点的难度,更易出现逻辑问题。所以尽量使用安全模式。

    这个简单了,可以想像一下TreeView和TreeNode基本上是这个意思了,将很多数据组织在一块。

    看代码:

    //CorpNode.h

    #pragma once
    #include <iostream>
    using std::string;
    class CCorpNode
    {
    public:
        CCorpNode();
        CCorpNode(string _name, string _pos, int _salary);
        virtual ~CCorpNode(void);
        virtual string GetInfo();
        void SetParent(CCorpNode *_pParent);
        CCorpNode * GetParent();
        virtual bool IsLeaf() = 0;
    private:
        string m_name;
        string m_position;
        int m_salary;
    protected:
        bool m_isLeaf;
        CCorpNode *m_pParent;
    };

    //CorpNode.cpp

    #include "StdAfx.h"
    #include "CorpNode.h"
    #include "..CommonDeclareConvert.h"
    CCorpNode::CCorpNode(void)
    {
        m_name = "";
        m_position = "";
        m_salary = 0;
    }
    CCorpNode::CCorpNode(string _name, string _pos, int _salary) : m_name(_name), m_position(_pos), m_salary(_salary)
    {
    }
    CCorpNode::~CCorpNode(void)
    {
    }
    string CCorpNode::GetInfo()
    {
        string info = "";
        info.append("姓名:");
        info.append(this->m_name);
        info.append(" 职位:");
        info.append(this->m_position);
        info.append(" 薪水:");
        info.append(CConvert::ToString(this->m_salary));
        return info;
    }
    void CCorpNode::SetParent( CCorpNode *_parent )
    {
        this->m_pParent = _parent;
    }
    CCorpNode * CCorpNode::GetParent()
    {
        return this->m_pParent;
    }

    //BranchNode.h

    #pragma once
    #include "corpnode.h"
    #include "CorpNode.h"
    #include <vector>
    #include <iostream>
    using std::vector;
    using std::string;
    class CBranchNode :
        public CCorpNode
    {
    public:
        CBranchNode(void);
        CBranchNode(string name, string pos, int salary);
        ~CBranchNode(void);
        void Add(CCorpNode *pcorpNode);
        vector<CCorpNode*> GetSubordinateInfo();
        bool IsLeaf();
    private:
        vector<CCorpNode*> m_subordinateList;
    };
    //BranchNode.cpp

    #include "StdAfx.h"
    #include "BranchNode.h"
    CBranchNode::CBranchNode(void)
    {
        m_isLeaf = false;
    }
    CBranchNode::CBranchNode( string name, string pos, int salary ) : CCorpNode(name, pos, salary)
    {
        m_isLeaf = false;
    }
    CBranchNode::~CBranchNode(void)
    {
    }
    void CBranchNode::Add( CCorpNode *pcorpNode )
    {
        pcorpNode->SetParent(this);
        m_subordinateList.push_back(pcorpNode);
    }
    vector<CCorpNode*> CBranchNode::GetSubordinateInfo()
    {
        return this->m_subordinateList;
    }
    bool CBranchNode::IsLeaf()
    {
        return m_isLeaf;
    }

    //LeafNode.h

    #pragma once
    #include "corpnode.h"
    class CLeafNode :
        public CCorpNode
    {
    public:
        CLeafNode(void);
        CLeafNode(string name, string pos, int salary);
        ~CLeafNode(void);
        bool IsLeaf();
    };
    //LeafNode.cpp

    #include "StdAfx.h"
    #include "LeafNode.h"
    CLeafNode::CLeafNode(void)
    {
        m_isLeaf = true;
    }
    CLeafNode::CLeafNode( string name, string pos, int salary ) : CCorpNode(name, pos, salary)
    {
        m_isLeaf = true;
    }
    CLeafNode::~CLeafNode(void)
    {
    }
    bool CLeafNode::IsLeaf()
    {
        return m_isLeaf;
    }

    曾经开发过一款Gantt图的控件,采用的就是这种模式,有GanttView和GanttNode两个类,IGanttNode一个接口,GanttScale标尺类。GanttView负责显示,GanttNode是实现类,只有一个GanttNode类来实现数据的组合,IGanttNode指示了一个IGanttNodeCollection属性来记录所有下级结点的集合。算是实践中的一个应用吧。

  • 相关阅读:
    用javascript实现简单的用户登录验证
    JS创建数组的三种方法
    JS中的数据类型
    原始值和引用值
    html为什么用雪碧图的优缺点
    html,将元素水平,垂直居中的四种方式
    使用display:none和visibility:hidden隐藏的区别
    jsonview注解、RequestBody 、拦截
    02.实现图形验证码
    spring注解使用
  • 原文地址:https://www.cnblogs.com/liaocheng/p/4361467.html
Copyright © 2020-2023  润新知