点点滴滴


         从点开始绘制自己的程序人生
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Abstract Factory

Posted on 2006-10-29 15:00  点点滴滴  阅读(162)  评论(0编辑  收藏  举报
// Abstract Factory pattern -- Real World example

using System;

namespace DoFactory.GangOfFour.Abstract.RealWorld
{
  // MainApp test application

  class MainApp
  {
    public static void Main()
    {
      // Create and run the Africa animal world
      ContinentFactory africa = new AfricaFactory();
      AnimalWorld world = new AnimalWorld(africa);
      world.RunFoodChain();

      // Create and run the America animal world
      ContinentFactory america = new AmericaFactory();
      world = new AnimalWorld(america);
      world.RunFoodChain();

      // Wait for user input
      Console.Read();
    }
  }

  // "AbstractFactory"

  abstract class ContinentFactory
  {
    public abstract Herbivore CreateHerbivore();
    public abstract Carnivore CreateCarnivore();
  }

  // "ConcreteFactory1"

  class AfricaFactory : ContinentFactory
  {
    public override Herbivore CreateHerbivore()
    {
      return new Wildebeest();
    }
    public override Carnivore CreateCarnivore()
    {
      return new Lion();
    }
  }

  // "ConcreteFactory2"

  class AmericaFactory : ContinentFactory
  {
    public override Herbivore CreateHerbivore()
    {
      return new Bison();
    }
    public override Carnivore CreateCarnivore()
    {
      return new Wolf();
    }
  }

  // "AbstractProductA"

  abstract class Herbivore
  {
  }

  // "AbstractProductB"

  abstract class Carnivore
  {
    public abstract void Eat(Herbivore h);
  }

  // "ProductA1"

  class Wildebeest : Herbivore
  {
  }

  // "ProductB1"

  class Lion : Carnivore
  {
    public override void Eat(Herbivore h)
    {
      // Eat Wildebeest
      Console.WriteLine(this.GetType().Name +
        " eats " + h.GetType().Name);
    }
  }

  // "ProductA2"

  class Bison : Herbivore
  {
  }

  // "ProductB2"

  class Wolf : Carnivore
  {
    public override void Eat(Herbivore h)
    {
      // Eat Bison
      Console.WriteLine(this.GetType().Name +
        " eats " + h.GetType().Name);
    }
  }

  // "Client"

  class AnimalWorld
  {
    private Herbivore herbivore;
    private Carnivore carnivore;

    // Constructor
    public AnimalWorld(ContinentFactory factory)
    {
      carnivore = factory.CreateCarnivore();
      herbivore = factory.CreateHerbivore();
    }

    public void RunFoodChain()
    {
      carnivore.Eat(herbivore);
    }
  }
}