日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

设计模式C++实现——组合模式

發(fā)布時間:2024/4/15 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式C++实现——组合模式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

模式定義:

? ? ? ? 組合模式同意你將對象組合成樹形結(jié)構(gòu)來表現(xiàn)“總體/部分”層次結(jié)構(gòu)。組合能讓客戶以一致的方式處理個別對象以及對象組合。

? ? ? ? 這個模式可以創(chuàng)建一個樹形結(jié)構(gòu),在同一個結(jié)構(gòu)中處理嵌套菜單和菜單項組。通過菜單和項放在同樣結(jié)構(gòu)中,我們創(chuàng)建了一個“總體/部分”層次結(jié)構(gòu),即由菜單和菜單項組成的對象樹。使用組合結(jié)構(gòu),我們能把同樣的操作應(yīng)用在組合和個別對象上。換句話說,在大多數(shù)情況下,我們可以忽略對象組合和個別對象之間的區(qū)別。

模式結(jié)構(gòu):


Component:

為組合中的對象聲明接口;

在適當(dāng)情況下實現(xiàn)全部類共同擁有接口的缺省行為。

聲明一個接口用于訪問管理Component的子組件

在遞歸結(jié)構(gòu)中定義一個接口,用于訪問一個父部件。并在合適的情況下實現(xiàn)它

Leaf:

在組合中表示葉節(jié)點對象,葉節(jié)點沒有子節(jié)點,并定義其行為

Composite:

定義有子部件的那些部件的行為

存儲子部件

實現(xiàn)與子部件有關(guān)的操作

Client:

通過Component接口操作組合件和個別對象。

?

舉例:

? ? ? ? 在迭代器樣例中,我們希望在午餐餐單中添加一份跌點餐單,也就是說希望能讓甜點餐單變成午餐餐單的一個元素。

? ? ? ? 我們能夠用組合模式解決問題:一開始我們創(chuàng)建一個組件接口作為餐單和菜單項的共同接口。讓我們能夠用統(tǒng)一的做法來處理菜單和菜單項。

換句話說,我們能夠針對菜單或菜單項調(diào)用同樣的方法。然后實現(xiàn)菜單項和組合菜單組件,以及他們各自的方法。

UML設(shè)計:

編程實現(xiàn)及運行結(jié)果:

#include <iostream> #include <vector> #include <list> #include <string>using namespace std;//菜單和菜單項共同的組件 class MenuComponent { public:virtual void add(MenuComponent* menuComponent){throw exception("add error!");}virtual void remove(MenuComponent* menuComponent){throw exception("remove error!");}virtual MenuComponent* getChild(int i){throw exception("getChild error");}virtual string getName(){throw exception("getName error");}virtual string getDescription(){throw exception("getDescription error");}virtual double getPrice(){throw exception("getPrice error");}virtual void print(){throw exception("print error");} };//菜單項類 class MenuItem : public MenuComponent { public:MenuItem(){}MenuItem(string na, string descrip, double pric){name = na;description = descrip;price = pric;}string getName(){return name;}string getDescription(){return description;}double getPrice(){return price;}void print(){cout << " " << getName() << ", " << getPrice() <<" ---" << getDescription() << endl;} private:string name;string description;double price; }; //組合菜單類 class Menu : public MenuComponent { public:Menu(string nam, string descri){name = nam;description = descri;}void add(MenuComponent* pMenuComponent){pMenuComponents.push_back(pMenuComponent);}void remove(MenuComponent* pMenuComponent){vector<MenuComponent*>::iterator iter = pMenuComponents.begin();for(; iter!=pMenuComponents.end(); ++iter){if(*iter == pMenuComponent){pMenuComponents.erase(iter);}}}MenuComponent* getChild(int i){return pMenuComponents[i];}string getName(){return name;}string getDescription(){return description;}void print(){cout << endl << getName() << ", " << getDescription() << endl << "--------------" << endl;vector<MenuComponent*>::iterator iter = pMenuComponents.begin();while(iter != pMenuComponents.end()){MenuComponent* pMenuComponent = *iter;pMenuComponent->print();++iter;}} private:vector<MenuComponent*> pMenuComponents;string name;string description; };//服務(wù)生類 class Waitress { public:Waitress(MenuComponent* all_Menus){allMenus = all_Menus;}void printMenu(){allMenus->print();} private:MenuComponent* allMenus; }; //客戶代碼 int main() {MenuComponent* pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");MenuComponent* dinerMenu = new Menu("Diner MENU", "Lunch");MenuComponent* dessertMenu = new Menu("DESSERT MENU","Dessert of coure!");MenuComponent* allMenus = new Menu("ALL Menus", "All menus combined");allMenus->add(pancakeHouseMenu);allMenus->add(dinerMenu);dinerMenu->add(new MenuItem("Pasta","Spaheti with Sauce", 3.89));dinerMenu->add(dessertMenu);dessertMenu->add(new MenuItem("Apple Pie", "App pie with a cruse", 1.59));Waitress* waitress = new Waitress(allMenus);waitress->printMenu();return 0; }

運行結(jié)果:

?

ALLMenus,????? All menus combined

--------------

轉(zhuǎn)載于:https://www.cnblogs.com/lytwajue/p/6960969.html

總結(jié)

以上是生活随笔為你收集整理的设计模式C++实现——组合模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。