结构型设计模式---组合模式(Composite)
2014-08-12 14:07
543 查看
组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构。
Composite使得用户对单个对象和组合对象的使用具有一致性。
适用场景:
(1)所要表示的对象具有部分-整体的层次结构
(2)用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象
Composite.h
#ifndef _COMPOSITE_H_
#define _COMPOSITE_H_
#include <string>
#include <vector>
using std::string;
using std::vector;
class Component
{
public:
virtual ~Component() {};
Component(string , int);
virtual void print(int ) = 0;
protected:
string name_;
int salary_;
};
class Worker : public Component
{
public:
Worker(string, int);
virtual void print(int);
};
class Manager : public Component
{
public:
Manager(string, int);
void add(Component *);
virtual void print(int);
private:
vector<Component *> children_;
};
#endif
Composite.cpp
#include <iostream>
#include "Composite.h"
using std::cout;
using std::endl;
Component::Component(string name, int salary) : name_(name), salary_(salary)
{
}
Worker::Worker(string name, int salary) : Component(name, salary)
{
}
void Worker::print(int level)
{
for (int i = 0; i < level; ++i)
cout << "\t";
cout << "Worker : " << name_ << " salary : " << salary_ << endl;
}
Manager::Manager(string name, int salary) : Component(name, salary)
{
}
void Manager::add(Component *com)
{
children_.push_back(com);
}
void Manager::print(int level)
{
for (int i = 0; i < level; ++i)
cout << "\t";
cout << "Manager : " << name_ << " salary : " << salary_ << endl;
if (!children_.empty())
{
for (int j = 0; j < level; ++j)
cout << "\t";
cout << "Subordinates of " << name_ << ": " << endl;
++level;
int size = children_.size();
for (int k = 0; k < size; ++k)
children_[k]->print(level);
}
}
main.cpp
#include "Composite.h"
int main()
{
Manager direct("Jim", 1000);
Manager mgrDev("Jun", 100);
Manager mgrQA("Na", 80);
Worker qa("tao", 80);
Worker qe("ou", 60);
Worker pdev("zhen", 70);
Worker sdev("denny", 60);
direct.add(&mgrDev);
direct.add(&mgrQA);
mgrDev.add(&pdev);
mgrDev.add(&sdev);
mgrQA.add(&qa);
mgrQA.add(&qe);
direct.print(0);
return 0;
}
Makefile
cc=g++
exe=main
obj= main.o Composite.o
$(exe): $(obj)
$(cc) -o $(exe) $(obj)
main.o: main.cpp Composite.h
$(cc) -c main.cpp
Composite.o: Composite.cpp Composite.h
$(cc) -c Composite.cpp
clean:
rm -rf *o $(exe)
output:
Manager : Jim salary : 1000
Subordinates of Jim:
Manager : Jun salary : 100
Subordinates of Jun:
Worker : zhen salary : 70
Worker : denny salary : 60
Manager : Na salary : 80
Subordinates of Na:
Worker : tao salary : 80
Worker : ou salary : 60
Composite使得用户对单个对象和组合对象的使用具有一致性。
适用场景:
(1)所要表示的对象具有部分-整体的层次结构
(2)用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象
Composite.h
#ifndef _COMPOSITE_H_
#define _COMPOSITE_H_
#include <string>
#include <vector>
using std::string;
using std::vector;
class Component
{
public:
virtual ~Component() {};
Component(string , int);
virtual void print(int ) = 0;
protected:
string name_;
int salary_;
};
class Worker : public Component
{
public:
Worker(string, int);
virtual void print(int);
};
class Manager : public Component
{
public:
Manager(string, int);
void add(Component *);
virtual void print(int);
private:
vector<Component *> children_;
};
#endif
Composite.cpp
#include <iostream>
#include "Composite.h"
using std::cout;
using std::endl;
Component::Component(string name, int salary) : name_(name), salary_(salary)
{
}
Worker::Worker(string name, int salary) : Component(name, salary)
{
}
void Worker::print(int level)
{
for (int i = 0; i < level; ++i)
cout << "\t";
cout << "Worker : " << name_ << " salary : " << salary_ << endl;
}
Manager::Manager(string name, int salary) : Component(name, salary)
{
}
void Manager::add(Component *com)
{
children_.push_back(com);
}
void Manager::print(int level)
{
for (int i = 0; i < level; ++i)
cout << "\t";
cout << "Manager : " << name_ << " salary : " << salary_ << endl;
if (!children_.empty())
{
for (int j = 0; j < level; ++j)
cout << "\t";
cout << "Subordinates of " << name_ << ": " << endl;
++level;
int size = children_.size();
for (int k = 0; k < size; ++k)
children_[k]->print(level);
}
}
main.cpp
#include "Composite.h"
int main()
{
Manager direct("Jim", 1000);
Manager mgrDev("Jun", 100);
Manager mgrQA("Na", 80);
Worker qa("tao", 80);
Worker qe("ou", 60);
Worker pdev("zhen", 70);
Worker sdev("denny", 60);
direct.add(&mgrDev);
direct.add(&mgrQA);
mgrDev.add(&pdev);
mgrDev.add(&sdev);
mgrQA.add(&qa);
mgrQA.add(&qe);
direct.print(0);
return 0;
}
Makefile
cc=g++
exe=main
obj= main.o Composite.o
$(exe): $(obj)
$(cc) -o $(exe) $(obj)
main.o: main.cpp Composite.h
$(cc) -c main.cpp
Composite.o: Composite.cpp Composite.h
$(cc) -c Composite.cpp
clean:
rm -rf *o $(exe)
output:
Manager : Jim salary : 1000
Subordinates of Jim:
Manager : Jun salary : 100
Subordinates of Jun:
Worker : zhen salary : 70
Worker : denny salary : 60
Manager : Na salary : 80
Subordinates of Na:
Worker : tao salary : 80
Worker : ou salary : 60
相关文章推荐
- 面向对象设计模式之Composite组合模式(结构型)
- 设计模式-结构型-composite 组合
- JAVA设计模式(11) —<结构型>组合模式(Composite)
- 设计模式--组合模式Composite(结构型)
- 设计模式(七)组合模式Composite(结构型)
- 设计模式学习(结构型模式)—组合模式(Composite)
- 【设计模式】结构型模式之组合Composite
- 设计模式(七)组合模式Composite(结构型)
- 【结构型模式】组合模式(Composite)之23种java设计模式
- [设计模式-结构型]组合(Composite )
- 【设计模式基础】结构型模式 - 5 - 组合(Composite)
- 设计模式(8)-结构型-组合模式(Composite)
- 设计模式(八):组合模式Composite(结构型模式)
- [设计模式]Composite(组合)——对象结构型模式
- 重学设计模式 - Composite(组合)模式 - 结构型
- C#面向对象设计模式学习笔记(8) - Composite 组合模式(结构型模式)
- 设计模式学习之组合模式(Composite,结构型模式)(10)
- 设计模式中结构型模式(三)组合模式(Composite)
- 设计模式-结构型模式:组合模式 Composite
- 设计模式之十一:Composite(组合)—对象结构型模式