设计模式之组合模式

【定义】组合模式(Composite),将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

【场景】现在我们自己实现几个简单的基本控件(如按钮Button、文本编辑框Label)等;而实际上窗口也算上一个控件,只是窗口可以有子窗口,但最终窗口还是得由以上的这些Button,Label组成。这里窗口是整体,基本控件是部分。那我们怎么把基本控件和窗口具有使用的一致性呢?不管你是Button还是窗口,有些UI用Widget(窗体)这个概念来形容。Widget使窗口也好,Button也好,都具有以下三个接口:(1)增加子Widget(2)移除子Widget(3)显示Widget(即自身)

【UML】


【代码】


#include <iostream>

using namespace std;

#define MAX_WIDGETS_NUM 0x05

class Widget
{
protected:
	int level;	//第几级窗口
public:
	Widget(int l):level(l){};
	virtual void add(Widget* w) = 0;
	virtual void remove(Widget* w) = 0;
	virtual void display() = 0;
};

class Window : public Widget
{
private:
	Widget* widgets[MAX_WIDGETS_NUM];
public:
	Window(int level):Widget(level)
	{
		memset(widgets, 0, sizeof(widgets));
	}
	void add(Widget* w)
	{
		for(int i = 0; i < MAX_WIDGETS_NUM; i++)
		{
			if(NULL == widgets[i])
			{
				widgets[i] = w;
				break;
			}
		}
	}
	void remove(Widget* w)
	{
		for(int i = 0; i < MAX_WIDGETS_NUM; i++)
		{
			if(w == widgets[i])
			{
				widgets[i] = NULL;
				break;
			}
		}
	}
	void display()
	{
		for(int i = 0; i < level; i++)
		{
			cout<<"    ";
		}
		cout<<"第"<<level<<"级窗口"<<endl;

		for(int i = 0; i < MAX_WIDGETS_NUM; i++)
		{
			if(NULL != widgets[i])
				widgets[i]->display();
		}
	}
};

class Button : public Widget
{
public:
	Button(int level):Widget(level)
	{
	}

	void add(Widget* w)
	{ 
	}
	void remove(Widget* w)
	{
	}

	void display()
	{
		for(int i = 0; i < level; i++)
		{
			cout<<"    ";
		}
		cout<<"第"<<level<<"级按钮"<<endl;
	}
};

int main()
{
	Window* wnd0 = new Window(0);

	Button* btn1 = new Button(1);
	Window* wnd1 = new Window(1);

	Window* wnd2_1 = new Window(2);
	Button* btn2 = new Button(2);
	Window* wnd2_2 = new Window(2);

	Button* btn3_1 = new Button(3);
	Button* btn3_2 = new Button(3);

	wnd0->add(btn1);
	wnd0->add(wnd1);

	wnd1->add(wnd2_1);
	wnd1->add(btn2);
	wnd1->add(wnd2_2);

	wnd2_1->add(btn3_1);
	wnd2_1->add(btn3_2);

	wnd0->display();
	
	//todo 将里面的子Widget remove掉,这里就不写了
	delete btn3_2;
	delete btn3_1;
	delete wnd2_2;
	delete btn2;
	delete wnd2_1;
	delete wnd1;
	delete btn1;
	delete wnd0;

	return 0;
}
 
【运行结果】



posted @ 2012-08-15 21:24  $逝水无痕$  阅读(122)  评论(0编辑  收藏  举报