Design Mode C ++ implementation-Combination Mode

Source: Internet
Author: User

Mode definition: The combination mode allows you to combine objects into a tree structure to express the "Overall/partial" hierarchy. The combination allows the customer to process individual objects and object combinations in a consistent manner.

In this mode, you can create a tree structure to process nested menus and menu item Groups in the same structure. By placing menus and items in the same structure, we create an "Overall/partial" hierarchy, which is an object tree consisting of menus and menu items. Using the combination structure, we can apply the same operation to the combination and individual objects. In other words, in most cases, we can ignore the differences between object combinations and individual objects.

Mode structure:

Component:

Declares interfaces for objects in a combination; <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 1 NrKyrWxx + m/9 s/Second/second + m8/second vcD4KPHA + second/Second/2z8LKtc/Wy/w8L3A + second + CjxwPtTa1 + second/second/ keys + keys/keys + keys/W0 + vX07K/keys + keys/astnX99fpus + 8/rrNuPax8LbUz/keys + keys/LT6xvfA/keys + LXjss21paOs0rK + keys/ export/export + m8/export/utcS5ss2svdO/export + 5Luwy7WjrM7Sw8e/export + m6z7LLtaXX6bz + o6zS1Lywy/export + export = "http://www.2cto.com/uploadfile/Collfiles/20140613/20140613092003187.jpg" alt = "\">

Programming implementation and execution results:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; // component 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") ;}}; // menu item 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 ;}; // composite Menu 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
     
      
: 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
      
        : Iterator iter = pMenuComponents. begin (); while (iter! = PMenuComponents. end () {MenuComponent * pMenuComponent = * iter; pMenuComponent-> print (); ++ iter;} private: vector
       
         PMenuComponents; string name; string description ;}; // waiter class Waitress {public: Waitress (MenuComponent * all_Menus) {allMenus = all_Menus;} void printMenu () {allMenus-> print ();} private: MenuComponent * allMenus;}; // client code int main () {MenuComponent * pancakeHouseMenu = new Menu ("pancake house menu ", "Breakfast"); MenuComponent * dinerMenu = new Menu ("Diner MENU", "Lunch"); MenuComponent * dessertMenu = new Menu ("DESS Ert 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 ;}
       
      
     
    
   
  
 


Execution result:

ALLMenus, All menus combined

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

Pancakehouse menu, Breakfast

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

DinerMENU, Lunch

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

Pasta, 3.89 --- Spaheti with Sauce

DESSERTMENU, Dessert of coure!

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

Apple Pie, 1.59 --- App pie with a cruse

Press any key to continue...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.