zoukankan      html  css  js  c++  java
  • 设计模式组成模式实现C++

    /*********************************
    *设计模式--组成模式实现
    *C++语言
    *Author:WangYong
    *Blog:http://www.cnblogs.com/newwy
    ********************************/
    #include <iostream>
    #include <vector>
    using namespace std;
    class Component
    {
    	public:
    	Component(){}
    	~Component(){}
    	virtual void Operation() = 0;
    	virtual void Add(const Component &){}
    	virtual void Remove(const Component&){}
    	virtual Component *GetChild(int){return 0;}
    };
    class Composite:public Component
    {
    	public:
    	Composite(){vector<Component*>::iterator itend = comVec.begin();}
    	~Composite(){}
    	void Operation()
    	{
    		vector<Component*>::iterator comIter = comVec.begin();
    		for(;comIter != comVec.end(); comIter++)
    		{
    			(*comIter)->Operation();
    		}
    	}
    	void Add(Component *com){comVec.push_back(com);}
    	void Remove(Component *com){comVec.erase(&com);}
    	Component * GetChild(int index){return comVec[index];}
    	private:
    	vector<Component*> comVec;
    };
    class Leaf:public Component
    {
    	public:
    	Leaf(){}
    	~Leaf(){}
    	void Operation(){cout<<"Leaf operation.."<<endl;}
    };
    int main()
    {
    	Leaf *l = new Leaf();
    	l->Operation();
    	Composite *com = new Composite();
    	com->Add(l);
    	com->Operation();
    	Component *ll = com->GetChild(0);
    	ll->Operation();
    	return 0;
    }
    
    
    
  • 相关阅读:
    linux tar order
    Linux驱动学习步骤(转载)
    汇编指令(转载)
    拓扑排序
    python 三维坐标图
    python 矩阵
    spring 之 IOC 依赖注入详解
    spring 下载
    Struts数据验证
    拦截器配置
  • 原文地址:https://www.cnblogs.com/newwy/p/1855210.html
Copyright © 2011-2022 走看看