Slash18

AbstractFactory

Jun 28th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1.  
  2. //Full tutorial on https://indiedevart.wordpress.com/
  3.  
  4. using System;
  5.  
  6. namespace AbstractFactory
  7. {
  8.  
  9.     interface IWeapon
  10.     {
  11.         string Item();
  12.     }
  13.     interface IArmor
  14.     {
  15.         string Item();
  16.     }
  17.     class Sword : IWeapon
  18.     {
  19.         public string Item()
  20.         {
  21.             return "Iron Sword";
  22.         }
  23.     }
  24.  
  25.     class Wand : IWeapon
  26.     {
  27.         public string Item()
  28.         {
  29.             return "Magic Wand";
  30.         }
  31.     }
  32.     class BodyArmor : IArmor
  33.     {
  34.         public string Item()
  35.         {
  36.             return "Iron Body Armor";
  37.         }
  38.     }
  39.  
  40.     class Cloak : IArmor
  41.     {
  42.         public string Item()
  43.         {
  44.             return "Magic Cloak";
  45.         }
  46.     }
  47.     interface IEnemyFactory
  48.     {
  49.         IWeapon GetWeapon();
  50.         IArmor GetArmor();
  51.     }
  52.  
  53.     class Mage : IEnemyFactory
  54.     {
  55.         public IWeapon GetWeapon()
  56.         {
  57.             return new Wand();
  58.         }
  59.  
  60.         public IArmor GetArmor()
  61.         {
  62.             return new Cloak();
  63.         }
  64.     }
  65.     class Warrior : IEnemyFactory
  66.     {
  67.         public IWeapon GetWeapon()
  68.         {
  69.             return new Sword();
  70.         }
  71.  
  72.         public IArmor GetArmor()
  73.         {
  74.             return new BodyArmor();
  75.         }
  76.     }
  77.     class Client
  78.     {
  79.  
  80.         IEnemyFactory factory=null;
  81.  
  82.         public void SpawnEnemy(string enemy)
  83.         {
  84.             if (enemy == "Warrior")
  85.             {
  86.                 factory = new Warrior();
  87.                 Console.WriteLine("New Warior equipment:");
  88.                 Console.WriteLine(factory.GetWeapon().Item());
  89.                 Console.WriteLine(factory.GetArmor().Item());
  90.                 Console.WriteLine("_____________________");
  91.             }
  92.             else if (enemy == "Mage")
  93.             {
  94.                 factory = new Mage();
  95.                 Console.WriteLine("New Mage equipment:");
  96.                 Console.WriteLine(factory.GetWeapon().Item());
  97.                 Console.WriteLine(factory.GetArmor().Item());
  98.                 Console.WriteLine("_____________________");
  99.             }
  100.             else
  101.             {
  102.                 Console.WriteLine("Wrong type");
  103.             }
  104.         }
  105.  
  106.  
  107.         static void Main(string[] args)
  108.         {
  109.             Client client = new Client();
  110.             client.SpawnEnemy("Mage");
  111.             client.SpawnEnemy("Warrior");
  112.             Console.Read();
  113.         }
  114.     }
  115. }
Add Comment
Please, Sign In to add comment