C++组合模式
来源:互联网 发布:linux系统如何使用 编辑:程序博客网 时间:2024/05/16 08:08
#include "stdafx.h"#include<iostream>#include<string>#include<vector>using namespace std;class Component{public:string m_strName;Component(string strName){this->m_strName = strName;}virtual void add(Component *com) = 0;virtual void dispaly(int nDepth) = 0;};class Leaf :public Component{public:Leaf(string strName) :Component(strName) {/*this->m_strName=strName;*/ }virtual void add(Component *com) {cout << "can't add leaf" << endl;}virtual void dispaly(int nDepth){string strTemp;for (int i = 0; i < nDepth; i++){strTemp += "-";}strTemp += this->m_strName;cout << strTemp << endl;}};class Composite :public Component{public:Composite(string strName) :Component(strName) {}virtual void add(Component *com) { m_pCom.push_back(com); }virtual void dispaly(int nDepth){string strTemp;for (int i = 0; i < nDepth; i++){strTemp += "-";}strTemp += this->m_strName;cout << strTemp << endl;vector<Component*>::iterator it = m_pCom.begin();while (it != m_pCom.end()){(*it)->dispaly(nDepth + 2);++it;}}private:vector<Component*>m_pCom;};void main(){Composite *p = new Composite("总经理");Composite *pM = new Composite("技术部门经理");p->add(pM);pM->add(new Leaf("开发人员A"));pM->add(new Leaf("开发人员B"));Composite *pS = new Composite("销售部门经理");p->add(pS);pS->add(new Leaf("销售人员C"));pS->add(new Leaf("销售人员D"));p->dispaly(1);};
0 0
- 【C++】Chapter15:组合模式
- Objective C--组合模式
- Objective C 组合模式
- C语言设计模式:组合模式
- 设计模式----组合模式(C#)
- C#--设计模式之组合模式
- OBJ-C组合模式学习笔记
- OBJ-C组合模式学习笔记
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
- C语言和设计模式(之组合模式)
- C语言和设计模式(之组合模式)
- C语言和设计模式(之组合模式)
- Objective-C 设计模式-组合模式 (Composite)
- 22.C语言和设计模式(之组合模式)
- C语言和设计模式(之组合模式)
- 5、C语言和设计模式(之组合模式)
- 设计模式——组合模式(C++)
- 组合模式
- Android多模块构建合并aar解决方案
- ubuntu安装包依赖问题
- Linux工程管理器make
- 爱情和心碎结伴而行
- hdu_1147 Pick-up sticks(线段相交)
- C++组合模式
- android-单击事件的响应方法
- CSS文字控制在二行 超出部分不显示
- Maven项目遇到OutOfMemoryError可以尝试的方法
- cuda编程入门示例2---CUDA environment initialization
- 衣带渐宽终不悔,为伊消得人憔悴--DbHelper增强版
- Android 混合开发之仿微信朋友圈
- 【数据结构】二叉树中任意两节点的最近公共祖先节点
- PHP 中的设计模式详解