设计模式C++ (Composite组合模式)
2015-11-20 09:48
302 查看
ref:http://www.cnblogs.com/wanggary/archive/2011/04/19/2021638.html
更多请看如下链接,该博主写的简单明了:
http://www.cnblogs.com/wanggary/category/294620.html http://www.cnblogs.com/justinw/archive/2006/11/28/574573.html
直接上源码:
#include <iostream>
#include <string>
#include <list>
#include <string>
#include <sstream>
using namespace std;
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;
};
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()
{
stringstream temp;
temp << this->m_salary;
string info = "";
info.append("姓名:");
info.append(this->m_name);
info.append("\t职位:");
info.append(this->m_position);
info.append("\t薪水:");
info.append(temp.str());
return info;
}
void CCorpNode::SetParent( CCorpNode *_parent )
{
this->m_pParent = _parent;
}
CCorpNode * CCorpNode::GetParent()
{
return this->m_pParent;
}
class CBranchNode :
public CCorpNode
{
public:
CBranchNode(void);
CBranchNode(string name, string pos, int salary);
~CBranchNode(void);
void Add(CCorpNode *pcorpNode);
list<CCorpNode*> GetSubordinateInfo();
bool IsLeaf();
private:
list<CCorpNode*> m_subordinateList;
};
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);
}
list<CCorpNode*> CBranchNode::GetSubordinateInfo()
{
return this->m_subordinateList;
}
bool CBranchNode::IsLeaf()
{
return m_isLeaf;
}
class CLeafNode :
public CCorpNode
{
public:
CLeafNode(void);
CLeafNode(string name, string pos, int salary);
~CLeafNode(void);
bool IsLeaf();
};
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;
}
void DoNew()
{
CLeafNode *one = new CLeafNode("one", "ooo", 1);
CLeafNode *two = new CLeafNode("two", "ooo", 2);
CLeafNode *three = new CLeafNode("three", "ooo", 3);
CBranchNode *Branch = new CBranchNode("Branch", "ooo", 100);
Branch->Add(one);
Branch->Add(two);
Branch->Add(three);
list<CCorpNode *> temp = Branch->GetSubordinateInfo();
list<CCorpNode *>::iterator it;
for ( it = temp.begin(); it != temp.end(); it++ )
{
cout << (*it)->GetParent()->GetInfo() << endl;
cout << (*it)->GetInfo() << endl;
}
for ( it = temp.begin(); it != temp.end(); it++ )
{
free(*it);
(*it) = NULL;
}
free(Branch);
Branch = NULL;
}
int
main(int argc, char **argv)
{
DoNew();
getchar();
return (0);
}
更多请看如下链接,该博主写的简单明了:
http://www.cnblogs.com/wanggary/category/294620.html http://www.cnblogs.com/justinw/archive/2006/11/28/574573.html
直接上源码:
#include <iostream>
#include <string>
#include <list>
#include <string>
#include <sstream>
using namespace std;
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;
};
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()
{
stringstream temp;
temp << this->m_salary;
string info = "";
info.append("姓名:");
info.append(this->m_name);
info.append("\t职位:");
info.append(this->m_position);
info.append("\t薪水:");
info.append(temp.str());
return info;
}
void CCorpNode::SetParent( CCorpNode *_parent )
{
this->m_pParent = _parent;
}
CCorpNode * CCorpNode::GetParent()
{
return this->m_pParent;
}
class CBranchNode :
public CCorpNode
{
public:
CBranchNode(void);
CBranchNode(string name, string pos, int salary);
~CBranchNode(void);
void Add(CCorpNode *pcorpNode);
list<CCorpNode*> GetSubordinateInfo();
bool IsLeaf();
private:
list<CCorpNode*> m_subordinateList;
};
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);
}
list<CCorpNode*> CBranchNode::GetSubordinateInfo()
{
return this->m_subordinateList;
}
bool CBranchNode::IsLeaf()
{
return m_isLeaf;
}
class CLeafNode :
public CCorpNode
{
public:
CLeafNode(void);
CLeafNode(string name, string pos, int salary);
~CLeafNode(void);
bool IsLeaf();
};
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;
}
void DoNew()
{
CLeafNode *one = new CLeafNode("one", "ooo", 1);
CLeafNode *two = new CLeafNode("two", "ooo", 2);
CLeafNode *three = new CLeafNode("three", "ooo", 3);
CBranchNode *Branch = new CBranchNode("Branch", "ooo", 100);
Branch->Add(one);
Branch->Add(two);
Branch->Add(three);
list<CCorpNode *> temp = Branch->GetSubordinateInfo();
list<CCorpNode *>::iterator it;
for ( it = temp.begin(); it != temp.end(); it++ )
{
cout << (*it)->GetParent()->GetInfo() << endl;
cout << (*it)->GetInfo() << endl;
}
for ( it = temp.begin(); it != temp.end(); it++ )
{
free(*it);
(*it) = NULL;
}
free(Branch);
Branch = NULL;
}
int
main(int argc, char **argv)
{
DoNew();
getchar();
return (0);
}
相关文章推荐
- 虚函数不应该是inlined(More Effective C++ 笔记)
- 22.c/c++程序员面试宝典-类
- Google C++ style
- 5个月iOS开发入门总结(C++转行iOS)
- 第10周项目1 二叉树算法库
- C/C++中产生随机数(rand,srand用法)
- C++实现单链表(2) 一些函数的实现
- C++之容器
- C++之容器(关联容器)
- c++中的关联容器
- C++之UML图示
- AVL树C++实现
- 【C语言】第一章 导言
- 【C语言】编写一个函数reverse_string(char * string)(递归实现
- 【C语言】【面试题】【笔试题】写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和
- strstr查找子字符串函数
- 【C语言】【面试题】【笔试题】编写一个函数实现n^k,使用递归实现
- 【C语言】在终端输入多行信息,找出包含“ould”的行,并打印改行
- VC++ 与C 语言的区别
- 【C语言】【面试题】【笔试题】模拟实现strstr函数