Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Full Tutorial on indiedevart.wordpress.com
- using System;
- namespace Factory
- {
- public interface IFactory
- {
- void Demage(int dmg);
- }
- public class EnemyType1 : IFactory
- {
- int _health=100;
- public void Demage(int dmg)
- {
- _health -= dmg;
- Console.WriteLine("EnemyType1 health: " + _health.ToString());
- }
- }
- public class EnemyType2 : IFactory
- {
- int _health = 200;
- public void Demage(int dmg)
- {
- _health -= dmg;
- Console.WriteLine("EnemyType2 health: " + _health.ToString());
- }
- }
- public abstract class EnemyFactory
- {
- public abstract IFactory InstantiateEnemy(int type);
- }
- public class InstantiateEnemyFactory : EnemyFactory
- {
- public override IFactory InstantiateEnemy(int type)
- {
- switch (type)
- {
- case 1:
- return new EnemyType1();
- case 2:
- return new EnemyType2();
- default:
- throw new ApplicationException(string.Format("Wrong number"));
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- int type = new Random().Next(1, 1000) % 2+1;
- EnemyFactory factory = new InstantiateEnemyFactory();
- IFactory newEnemy= factory.InstantiateEnemy(type);
- newEnemy.Demage(20);
- type = new Random().Next(1, 2000)%2+1;
- IFactory newEnemy2 = factory.InstantiateEnemy(type);
- newEnemy2.Demage(20);
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment