Design Pattern

旧城冷巷雨未停 提交于 2019-12-19 17:50:35

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

按照每天一个设计模式的节奏,今天进入Composite模式,这个模式在软件开发中还是很容易出现的,尤其界面控件,比如listview是由list和header两个控件组合而成. 当然了,逻辑领域例子也不少, 正如1+2 = 3一样,3 也是个整数,而且可以视为组合型.


这个模式的纠结点在于组合行为是托管在基类还是组合类, 其实就是add/remove/get操作的生命地点. 我个人比较喜欢类的纯粹,所以喜欢有一个Composite的次基类,诚如下面代码所示.


评价: 按照惯例评价, Composite应该算战斗级别, 在实际案例中, 组合内的逻辑是复杂的, 组合内对象一般也是有顺序和数目要求,同时对方法调用也是需要逻辑处理的, 一般可以看作一个复杂的多子对象的Decorator.

// Composite.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Composite;
class Component
{
public:
	virtual Composite* GetComposite(){ return NULL;}

	virtual void Method(){}
};

class Composite : public Component
{
	vector<Component*> _childs;
public:
	/// This is to make sure we have simple Base class, Don't put add/remove to Base.
	virtual Composite* GetComposite(){ return this;}

	virtual void Method(){
		for(int i = 0;i<_childs.size();i++){
			_childs[i]->Method();
		}
	}
	void Add (Component* child) { _childs.push_back(child);}
	void Remove (Component* child) { 
		vector<Component*>::iterator iter = find(_childs.begin(),_childs.end(),child);
		if(iter != _childs.end())
			_childs.erase(iter);
	}

};

class ComponentConcret1 : public Component
{
public:
	virtual void Method(){ cout << "this is concret one." << endl; }
};

class ComponentConcret2 : public Component
{
public:
	virtual void Method(){ cout << "this is concret two." << endl; }
};

int main(int argc, char* argv[])
{
	Component* com1 = new ComponentConcret1();
	com1->Method();
	//assert(NULL == com1->GetComposite());
	Component* com2 = new Composite();
	Composite* comp = com2->GetComposite();
	if(NULL != comp){
		comp->Add(com1);
		comp->Add(new ComponentConcret2);

		comp->Method();
	}

	return 0;
}


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!