設計模式C++實現——組合模式

來源:互聯網
上載者:User

標籤:階層   gravity   for   art   etc   建立   item   get   exception   

模式定義:        組合模式同意你將對象組合成樹形結構來表現“總體/部分”階層。組合能讓客戶以一致的方式處理個別對象以及對象組合。

        這個模式可以建立一個樹形結構,在同一個結構中處理嵌套菜單和功能表項目組。通過菜單和項放在同樣結構中,我們建立了一個“總體/部分”階層,即由菜單和功能表項目組成的對象樹。使用組合結構,我們能把同樣的操作應用在組合和個別對象上。換句話說,在大多數情況下,我們可以忽略對象組合和個別對象之間的區別。

模式結構:

Component:

為組合中的對象聲明介面;

在適當情況下實現全部類共同擁有介面的預設行為。

聲明一個介面用於訪問管理Component的子組件

在遞迴結構中定義一個介面,用於訪問一個父組件。並在合適的情況下實現它

Leaf:

在組合中表示分葉節點對象,分葉節點沒有子節點,並定義其行為

Composite:

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

儲存子組件

實現與子組件有關的操作

Client:

通過Component介面操作組合件和個別對象。

 

舉例:

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

        我們能夠用組合模式解決問題:一開始我們建立一個組件介面作為餐單和功能表項目的共同介面。讓我們能夠用統一的做法來處理菜單和功能表項目。

換句話說,我們能夠針對菜單或功能表項目調用同樣的方法。然後實現功能表項目和組合菜單組件,以及他們各自的方法。

UML設計:

編程實現及運行結果:

#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;};//服務生類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;}


運行結果:

 

ALLMenus,      All menus combined

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

設計模式C++實現——組合模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.