c++设计模式之Composite组合模式.txt
来源:互联网 发布:淘宝举报店铺 编辑:程序博客网 时间:2024/05/21 13:09
设计模式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 "..\CommonDeclare\Convert.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("\t职位:");
info.append(this->m_position);
info.append("\t薪水:");
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;
}
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 "..\CommonDeclare\Convert.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("\t职位:");
info.append(this->m_position);
info.append("\t薪水:");
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;
}
0 0
- c++设计模式之Composite组合模式.txt
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 设计模式之Composite(组合)
- 模式设计之组合Composite
- 设计模式之Composite组合设计模式
- 设计模式之组合模式(Composite Pattern)
- 设计模式之组合模式(Composite Pattern)
- 设计模式读书笔记之组合模式(Composite)
- 设计模式之组合模式(Composite)
- 设计模式之(八)组合模式Composite
- vue.js进阶之组件
- 对于ssh连接太慢的简单解决方案
- c++设计模式之Command命令模式.txt
- servlet (1)组成成分
- Linux下python matplotlib.pyplot在图像上显示中文的问题
- c++设计模式之Composite组合模式.txt
- c++设计模式之Decorator装饰模式.txt
- 基类和派生类的各种访问
- 动态获取权限, 以及事件处理
- Layout(drawable)文件太多怎么办
- c++设计模式之Facade门面模式.txt
- 搜索关键字杀一组进程
- 第26天 贪吃蛇项目源码
- 快速排序法的理解