Ejemplo n.º 1
0
int main()
{
	// 创建一个树形结构
	// 创建根节点
	Component *pRoot = new Composite("江湖公司(任我行)");

	// 创建分支
	Component *pDepart1 = new Composite("日月神教(东方不败)");
	pDepart1->Add(new Leaf("光明左使(向问天)"));
	pDepart1->Add(new Leaf("光明右使(曲洋)"));
	pRoot->Add(pDepart1);

	Component *pDepart2 = new Composite("五岳剑派(左冷蝉)");
	pDepart2->Add(new Leaf("嵩山(左冷蝉)"));
	pDepart2->Add(new Leaf("衡山(莫大)"));
	pDepart2->Add(new Leaf("华山(岳不群)"));
	pDepart2->Add(new Leaf("泰山(天门道长)"));
	pDepart2->Add(new Leaf("恒山(定闲师太)"));
	pRoot->Add(pDepart2);

	// 添加和删除叶子
	pRoot->Add(new Leaf("少林(方证大师)"));
	pRoot->Add(new Leaf("武当(冲虚道长)"));
	Component *pLeaf = new Leaf("青城(余沧海)");
	pRoot->Add(pLeaf);

	// 小丑,直接裁掉
	pRoot->Remove(pLeaf);

	// 递归地显示组织架构
	pRoot->Operation(1);

	// 删除分配的内存
	SAFE_DELETE(pRoot);

	getchar();

	return 0;
}
Ejemplo n.º 2
0
int main()
{
    Component* cmp;
    Leaf* lfp;
    Leaf lf;
    Composite cs;

    cmp = &lf;
    cmp->Add(cmp);
    cmp->Remove(cmp);

    lfp = &lf;

    lfp->Add(cmp);  //compile error
    lf.Add(cmp);    //compile error

    return 0;
}
Ejemplo n.º 3
0
void main()
{

	try
	{

		Component *			HeadComponent;
		Component *			CurrComponent;
		Component *			SubComponent;
		Component *			SubSubComponent;

		ComponentIterator * CompIter;
		int x;
		int y;
		int z;
		char name[100] = {"Object level"};
		char actual_name[100];
		char add_string[100];

		// build it up.....

		HeadComponent = new Composite("Head");
		CurrComponent = HeadComponent;

		for (x = 0; x<10 ;x++)
		{
			strcpy(actual_name,name);
			sprintf(add_string,"1-%d",x);
			strcat(actual_name,add_string);

			if(x == 2 || x == 4)
			{
				strcpy(actual_name,name);
				sprintf(add_string,"1-%d-%d",x,y);
				strcat(actual_name,add_string);

				SubComponent = new Composite(actual_name);
				CurrComponent->Add(SubComponent);
				for (y = 0; y < 10; y++)
				{
					if (y == 7 || y == 9)
					{
						SubSubComponent = new Composite(actual_name);
						SubComponent->Add(SubSubComponent);


					}
					else
					{
						SubComponent->Add(new Component(actual_name));
					}
				}

			}
			else
			{
				CurrComponent->Add(new Component(actual_name));
    		}
		}

		HeadComponent->Operation();

		// test iterator operation!
		if (HeadComponent->GetChild())
		{
			CompIter = HeadComponent->CreateIterator();

			while (! CompIter->IsDone())
			{
				CompIter->CurrentItem()->PrintName();
				(*CompIter)++;
			}
		}

		delete CompIter;

		delete HeadComponent;
	}
	catch(EXCEPTION_CODE ec)
	{
		
		switch (ec)
		{
		case EXC_MEMORY_ALLOC_FAILURE_LISTCLASS :
			cout << "Exception:" << "mem alloc failure.. list class" << endl;
			break;

		case EXC_HEAP_ALLOC_FAILURE :
			cout << "Exception:" << "Heap allocation failure!" << endl;
			break;

		case EXC_BAD_ADD_COMPONENT :
			cout << "Exception:" << "Tried to add w/ component object" << endl;
			break;

		case EXC_BAD_REMOVE_COMPONENT :
			cout << "Exception:" << "Tried to remove w/ component object" << endl;
			break;

		case EXC_BAD_COUNT_CALL :
			cout << "Exception:" << "Tried to get count on a component object" << endl;
			break;

		case EXC_BAD_GET_CALL :
			cout << "Exception:" << "Tried to get a component object from a component object" << endl;
			break;

		case EXC_CANT_CREATE_ITERATOR :
			cout << "Exception:" << "Can't create component for object of this type!" << endl;
			break;


		default:
			cout << "Exception: " << " no msg for this exception" << endl;

		}
	}
}