结构型设计模式---组合模式(Composite)
来源:互联网 发布:java实现简单图形界面 编辑:程序博客网 时间:2024/05/16 08:36
组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构。
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 )
- 设计模式-结构型-composite 组合
- (C++设计模式) ------Composite 组合模式-- 结构型模式
- 设计模式-结构型模式:组合模式 Composite
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(七)组合模式Composite(结构型)
- 重学设计模式 - Composite(组合)模式 - 结构型
- 设计模式(8)-结构型-组合模式(Composite)
- 设计模式(七)组合模式Composite(结构型)
- 【设计模式基础】结构型模式 - 5 - 组合(Composite)
- 设计模式--组合模式Composite(结构型)
- 设计模式(结构型)之组合模式(Composite Pattern)
- 设计模式(九)组合模式(Composite)-结构型
- JAVA设计模式(11) —<结构型>组合模式(Composite)
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(3)-结构型-组合模式(Composite)
- 【设计模式】结构型模式之组合Composite
- 安卓系统可删程序列表
- 打印调试技术 printk klogd dmesg
- MP3播放器如果你在移动
- MemoryUp临先后被评为“一个非常必要的应用程序的黑莓用户通过黑莓应用程序窝
- Linux的本地DNS缓存服务器
- 结构型设计模式---组合模式(Composite)
- 如何保障传真的证明力
- Lourens马雷首席执行官珠宝顾问南非,在赋权乡镇配饰品培训
- 数组算法的总结
- Linux的开源操作系统
- codeforces 451 D.Count Good Substrings (思维)
- Linux和高IO等待
- ios delegate代理,多个/不同/两个view之间传递数据,传递批量数据,多个数据
- Linux中,自由主义和美国