Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Full tutorial on https://indiedevart.wordpress.com/
- using System;
- namespace AbstractFactory
- {
- interface IWeapon
- {
- string Item();
- }
- interface IArmor
- {
- string Item();
- }
- class Sword : IWeapon
- {
- public string Item()
- {
- return "Iron Sword";
- }
- }
- class Wand : IWeapon
- {
- public string Item()
- {
- return "Magic Wand";
- }
- }
- class BodyArmor : IArmor
- {
- public string Item()
- {
- return "Iron Body Armor";
- }
- }
- class Cloak : IArmor
- {
- public string Item()
- {
- return "Magic Cloak";
- }
- }
- interface IEnemyFactory
- {
- IWeapon GetWeapon();
- IArmor GetArmor();
- }
- class Mage : IEnemyFactory
- {
- public IWeapon GetWeapon()
- {
- return new Wand();
- }
- public IArmor GetArmor()
- {
- return new Cloak();
- }
- }
- class Warrior : IEnemyFactory
- {
- public IWeapon GetWeapon()
- {
- return new Sword();
- }
- public IArmor GetArmor()
- {
- return new BodyArmor();
- }
- }
- class Client
- {
- IEnemyFactory factory=null;
- public void SpawnEnemy(string enemy)
- {
- if (enemy == "Warrior")
- {
- factory = new Warrior();
- Console.WriteLine("New Warior equipment:");
- Console.WriteLine(factory.GetWeapon().Item());
- Console.WriteLine(factory.GetArmor().Item());
- Console.WriteLine("_____________________");
- }
- else if (enemy == "Mage")
- {
- factory = new Mage();
- Console.WriteLine("New Mage equipment:");
- Console.WriteLine(factory.GetWeapon().Item());
- Console.WriteLine(factory.GetArmor().Item());
- Console.WriteLine("_____________________");
- }
- else
- {
- Console.WriteLine("Wrong type");
- }
- }
- static void Main(string[] args)
- {
- Client client = new Client();
- client.SpawnEnemy("Mage");
- client.SpawnEnemy("Warrior");
- Console.Read();
- }
- }
- }
Add Comment
Please, Sign In to add comment