设计模式——组合模式(C++)
来源:互联网 发布:供应商数据库表格 编辑:程序博客网 时间:2024/06/05 15:04
/*****************************************Copyright (c) 2016 Jingshuang Hu@filename:Component.h@datetime:2016.09.19@author:HJS@e-mail:jingshuang_hu@163.com@blog:http://blog.csdn.net/hujingshuang*****************************************/#ifndef _COMPONENT_H#define _COMPONENT_H#include <list>#include <string>// 定义接口class Component {public: virtual void operation() = 0; virtual void add(Component* component) = 0; virtual void remove(Component* component) = 0; virtual Component* getChild(int index) = 0;};// 叶节点,继承并实现了operation方法// 也继承了add、remove、getChild等操作,但叶节点没有孩子,因此这些方法对叶节点无意义。class Leaf : public Component {private: std::string name;public: Leaf(std::string name); virtual void operation(); virtual void add(Component* component); virtual void remove(Component* component); virtual Component* getChild(int index);};// 定义组件,继承并实现了基类中的所有方法,组件可以添加叶节点或新的组件class Composite : public Component {private: std::string name; std::list<Component*> *lst;public: Composite(std::string name); virtual void operation(); virtual void add(Component* component); virtual void remove(Component* component); virtual Component* getChild(int index);};#endif // _COMPONETN_H
/*****************************************Copyright (c) 2016 Jingshuang Hu@filename:Component.cpp@datetime:2016.09.19@author:HJS@e-mail:jingshuang_hu@163.com@blog:http://blog.csdn.net/hujingshuang*****************************************/#include "Component.h"#include <iostream>#include <list>#include <string>using namespace std;// 抽象接口void Component::operation() {}void Component::add(Component* component) {}void Component::remove(Component* component) {}Component* Component::getChild(int index) { return NULL;}// 叶节点Leaf::Leaf(string name) { this->name = name;}void Leaf::operation() { cout << "Leaf:" + name + ":operation" << endl;}void Leaf::add(Component* component) {}void Leaf::remove(Component* component) {}Component* Leaf::getChild(int index) { return NULL;}// 组件Composite::Composite(string name) { this->name = name; lst = new list<Component*>;}void Composite::operation() { cout << "Composite:" + name + ":operation" << endl; list<Component*>::iterator it = lst->begin(); for (; it != lst->end(); it++) { (*it)->operation(); }}void Composite::add(Component* component) { // 添加叶节点或组件 lst->push_back(component);}void Composite::remove(Component* component) { // 移除叶节点或组件 lst->remove(component);}Component* Composite::getChild(int index) { if (index < 0 || index >= lst->size()) { return NULL; } list<Component*>::iterator it = lst->begin(); for (int i = 0; i < index; i++){ it++; } return *it;}
/*****************************************Copyright (c) 2016 Jingshuang Hu@filename:main.cpp@datetime:2016.09.19@author:HJS@e-mail:jingshuang_hu@163.com@blog:http://blog.csdn.net/hujingshuang*****************************************/#include <iostream>#include "Component.h"using namespace std;// 简易层次结构// root +---->leafA // +----->comA +----->leafB1// +----->leafB2// +----->comB +----->leafC// +----->comC +----->leafD// 以UI设计的角度来看,非常形象,很好理解。int main() { Component* root = new Composite("root"); // 可看做Panel Leaf* leafA = new Leaf("A"); // 可看做控件 Leaf* leafB1 = new Leaf("B1"); Leaf* leafB2 = new Leaf("B2"); Leaf* leafC = new Leaf("C"); Leaf* leafD = new Leaf("D"); Composite* comA = new Composite("A"); // 可看做subPanel Composite* comB = new Composite("B"); Composite* comC = new Composite("C"); root->add(leafA); root->add(comA); comA->add(leafB1); comA->add(leafB2); comA->add(comB); comB->add(leafC); comB->add(comC); comC->add(leafD); root->operation(); comA->remove(comB); root->operation(); Component* com = comA->getChild(1); if (com) { com->operation(); } system("pause"); return 0;}// 组合模式:允许将对象组合成树形结构来表现“整体-部分”层次结构。组合能让客户以一致的方式处理个别对象以及组合。
1 0
- 设计模式——组合模式(C++)
- 设计模式—组合模式(十五)
- 设计模式—组合模式
- 设计模式—组合模式
- 设计模式----组合模式(C#)
- 设计模式——组合设计模式
- 设计模式组合——命令模式组合工厂模式
- 每日设计模式——组合模式
- 设计模式——组合模式
- Java设计模式——组合模式
- java设计模式——组合模式
- 设计模式——组合模式
- 设计模式——组合模式
- 设计模式——组合模式
- 设计模式——组合模式
- Python设计模式——组合模式
- 设计模式8——组合模式
- C++设计模式——组合模式
- oracle常用数据函数sql
- 五、1文件系统中的函数PTR_ERR IS_ERR ERR_PTR
- 去掉字符串中不需要的字符
- httpclient post stream
- Entity Framework 中的Code First
- 设计模式——组合模式(C++)
- delta法则(梯度下降)
- 关于 Android Apk 反编译
- 在线客服openfire设置
- 短信猫二次开发
- Xcode8
- JDBC连接数据库七步走
- python socket简述
- mysql中 where in 用法详解