Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1.  
  2.                  
  3. using System;
  4.  
  5. namespace DoFactory.GangOfFour.Abstract.RealWorld
  6. {
  7.   /// <summary>
  8.   /// MainApp startup class for Real-World
  9.   /// Abstract Factory Design Pattern.
  10.   /// </summary>
  11.   class MainApp
  12.   {
  13.     /// <summary>
  14.     /// Entry point into console application.
  15.     /// </summary>
  16.     public static void Main()
  17.     {
  18.       // Create and run the African animal world
  19.       ContinentFactory africa = new AfricaFactory();
  20.       AnimalWorld world = new AnimalWorld(africa);
  21.       world.RunFoodChain();
  22.  
  23.       // Create and run the American animal world
  24.       ContinentFactory america = new AmericaFactory();
  25.       world = new AnimalWorld(america);
  26.       world.RunFoodChain();
  27.  
  28.       // Wait for user input
  29.       Console.ReadKey();
  30.     }
  31.   }
  32.  
  33.  
  34.   /// <summary>
  35.   /// The 'AbstractFactory' abstract class
  36.   /// </summary>
  37.   abstract class ContinentFactory
  38.   {
  39.     public abstract Herbivore CreateHerbivore();
  40.     public abstract Carnivore CreateCarnivore();
  41.   }
  42.  
  43.   /// <summary>
  44.   /// The 'ConcreteFactory1' class
  45.   /// </summary>
  46.   class AfricaFactory : ContinentFactory
  47.   {
  48.     public override Herbivore CreateHerbivore()
  49.     {
  50.       return new Wildebeest();
  51.     }
  52.     public override Carnivore CreateCarnivore()
  53.     {
  54.       return new Lion();
  55.     }
  56.   }
  57.  
  58.   /// <summary>
  59.   /// The 'ConcreteFactory2' class
  60.   /// </summary>
  61.   class AmericaFactory : ContinentFactory
  62.   {
  63.     public override Herbivore CreateHerbivore()
  64.     {
  65.       return new Bison();
  66.     }
  67.     public override Carnivore CreateCarnivore()
  68.     {
  69.       return new Wolf();
  70.     }
  71.   }
  72.  
  73.   /// <summary>
  74.   /// The 'AbstractProductA' abstract class
  75.   /// </summary>
  76.   abstract class Herbivore
  77.   {
  78.   }
  79.  
  80.   /// <summary>
  81.   /// The 'AbstractProductB' abstract class
  82.   /// </summary>
  83.   abstract class Carnivore
  84.   {
  85.     public abstract void Eat(Herbivore h);
  86.   }
  87.  
  88.   /// <summary>
  89.   /// The 'ProductA1' class
  90.   /// </summary>
  91.   class Wildebeest : Herbivore
  92.   {
  93.   }
  94.  
  95.   /// <summary>
  96.   /// The 'ProductB1' class
  97.   /// </summary>
  98.   class Lion : Carnivore
  99.   {
  100.     public override void Eat(Herbivore h)
  101.     {
  102.       // Eat Wildebeest
  103.       Console.WriteLine(this.GetType().Name +
  104.         " eats " + h.GetType().Name);
  105.     }
  106.   }
  107.  
  108.   /// <summary>
  109.   /// The 'ProductA2' class
  110.   /// </summary>
  111.   class Bison : Herbivore
  112.   {
  113.   }
  114.  
  115.   /// <summary>
  116.   /// The 'ProductB2' class
  117.   /// </summary>
  118.   class Wolf : Carnivore
  119.   {
  120.     public override void Eat(Herbivore h)
  121.     {
  122.       // Eat Bison
  123.       Console.WriteLine(this.GetType().Name +
  124.         " eats " + h.GetType().Name);
  125.     }
  126.   }
  127.  
  128.   /// <summary>
  129.   /// The 'Client' class
  130.   /// </summary>
  131.   class AnimalWorld
  132.   {
  133.     private Herbivore _herbivore;
  134.     private Carnivore _carnivore;
  135.  
  136.     // Constructor
  137.     public AnimalWorld(ContinentFactory factory)
  138.     {
  139.       _carnivore = factory.CreateCarnivore();
  140.       _herbivore = factory.CreateHerbivore();
  141.     }
  142.  
  143.     public void RunFoodChain()
  144.     {
  145.       _carnivore.Eat(_herbivore);
  146.     }
  147.   }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement