Composite Pattern (10)

组合模式:

       将对象组织成树形结构的形式来表现部分和整体的继承关系。组合模式让客户将独立的叶节点对象和枝节点对象进行统一处理。

示例代码:

using System;

using System.Text;

using System.IO;

using System.Collections.Generic;

 

namespace Hello

{

   //self-define Exception

   public class UnsopportedOperationException : ApplicationException

   {

     

   }

   //Node abstract class

   public abstract class MenuComponent

   {

      public virtual void Add(MenuComponent menuComponent)

      {

        throw new UnsopportedOperationException();

      }

      public virtual void Remove(MenuComponent menuComponent)

      {

        throw new UnsopportedOperationException();

      }

      public virtual MenuComponent GetChild(int i)

      {

        throw new UnsopportedOperationException();

      }

      public virtual string GetName()

      {

        throw new UnsopportedOperationException();

      }

      public virtual string GetDescription()

      {

        throw new UnsopportedOperationException();

      }

      public virtual double GetPrice()

      {

        throw new UnsopportedOperationException();

      }

      public virtual bool IsVegetarian()

      {

        throw new UnsopportedOperationException();

      }

      public virtual void Print()

      {

        throw new UnsopportedOperationException();

      }

   }

   //Leaf Node Clas

   public class MenuItem : MenuComponent

   {

      string name;

      string description;

      bool vegetarian;

      double price;

      public MenuItem(string name, string description, bool vegetarian, double price)

      {

        this.name = name;

        this.description = description;

        this.vegetarian = vegetarian;

        this.price = price;

      }

      public override string GetName()

      {

        return name;

      }

      public override string GetDescription()

      {

        return description;

      }

      public override double GetPrice()

      {

        return price;

      }

      public override bool IsVegetarian()

      {

        return vegetarian;

      }

      public override void Print()

      {

        Console.Write(GetName());

        if (IsVegetarian())

        {

           Console.WriteLine("(V)");

         }

        Console.Write("--" + GetPrice());

        Console.WriteLine("--" + GetDescription());

      }

   }

   //Branch Node Class

   public class Menu : MenuComponent

   {

      List<MenuComponent> menuComponents = new List<MenuComponent>();

      string name;

      string description;

      public Menu(string name, string description)

      {

        this.name = name;

        this.description = description;

      }

      public override void Add(MenuComponent menuComponent)

      {

        menuComponents.Add(menuComponent);

      }

      public override void Remove(MenuComponent menuComponent)

      {

        menuComponents.Remove(menuComponent);

      }

      public override MenuComponent GetChild(int i)

      {

        return menuComponents[i];

      }

      public override string GetName()

      {

        return name;

      }

      public override string GetDescription()

      {

        return description;

      }

      public override void Print()

      {

        Console.Write("\n" + GetName());

        Console.WriteLine(", " + GetDescription());

        Console.WriteLine("-----------------------");

      }

   }

 

   class Program

   {

      static void Main(string[] args)

      {

        MenuComponent pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");

        MenuComponent dinerMenu = new Menu("DINER MENU", "Lunch");

        MenuComponent cafeMenu = new Menu("CAFE MENU", "Dinner");

        MenuComponent dessertMenu = new Menu("DESSERT MENU", "Dessert of course!");

 

        MenuComponent allMenus = new Menu("ALL MENUS", "All menus Combined");

        allMenus.Add(pancakeHouseMenu);

        allMenus.Add(dinerMenu);

        allMenus.Add(cafeMenu);

 

        dinerMenu.Add(new MenuItem("Pasta","xxx xxx xx x xx x xxx",true,3.89));

        dinerMenu.Add(dessertMenu);

        dessertMenu.Add(new MenuItem("Apple Pie", "Apple Pie with xx xxx x", true, 1.59));

      }

   }

}

 

posted @ 2011-10-06 12:26  Erebus_NET  阅读(107)  评论(0编辑  收藏  举报