lundi 12 juin 2017

Abstract factory desing pattern C#

I am trying to make some easy to understand example of Abstract Factory Design. To my understanding so far it is basically factory which creates other factories which creates final objects.

  1. Is the following example a good example of Abstract Factory Design Pattern or not. If not why?
  2. Do you have any better example?

    using System;
    
    namespace mySpace
    {
    interface INPC
    {
        string Name { get; set; }
        int HP { get; set; }
        void talk();
    }
    
    interface INPC_factory
    {
        INPC Spawn();
    }
    
    public static class Setting
    {
        public static string randomEncounter = "Fighter";
        public static string location = "North";
        public static string difficulty = "medium";
        public static string playerWeapon = null;
    }
    
    class Program
    { 
        static void Main(string[] args)
        {
            INPC_factory factory = NPC_Factory.createFactoryForCurrentSetting();
            INPC NPC = factory.Spawn();
            NPC.talk();
        }
    }
    
    public class TraderFactory : INPC_factory
    {
        public TraderFactory() { Console.WriteLine("TraderFactory Created"); }
        INPC INPC_factory.Spawn()
        {
            if(Setting.location == "North")
                return new Trader("Viking trader", 150);
            else
                return new Trader("Common trader", 100);
        }
    }
    
    public class Trader : INPC
    {
        public string Name { get; set; }
        public int HP { get; set; }
    
        public Trader(string Name,int HP)
        {
            this.Name = Name;
            this.HP = HP;
            Console.WriteLine("Trader Created");
        }
    
        public void talk()
        {
            Console.WriteLine("Hello I am a trader.");
        }
    }
    
    public class FighterFactory : INPC_factory
    {
        public FighterFactory() { Console.WriteLine("FighterFactory Created"); }
        INPC INPC_factory.Spawn()
        {
            if (Setting.location == "North" & Setting.difficulty == "hard")
                return new Boss("Ragnar Lodbrok", 1000);
            if (Setting.location == "North")
                return new Fighter("Viking", 100);
            else
                return new Fighter("Common fighter", 100);
        }
    }
    
    public class Fighter: INPC
    {
        public string Name { get; set; }
        public int HP { get; set; }
    
        public Fighter(string Name, int HP)
        {
            this.Name = Name;
            this.HP = HP;
            Console.WriteLine("Fighter Created");
        }
    
        public void talk()
        {
            Console.WriteLine("Get ready to fight!");
        }
    }
    
    public class Boss : INPC
    {
        public string Name { get; set; }
        public int HP { get; set; }
    
        public Boss(string Name, int HP)
        {
            this.Name = Name;
            this.HP = HP;
            Console.WriteLine("Boss Created");
        }
    
        public void talk()
        {
            if(Setting.playerWeapon != null)
                Console.WriteLine("Let gods decide who shall be the king");
            else
                Console.WriteLine("Are you challenging me? If you're challenging me, you have to have a sword in your hand.");
        }
    }
    
    class NPC_Factory
    {
        public static INPC_factory createFactoryForCurrentSetting()
        {
            if (Setting.randomEncounter == "Fighter")
                return new FighterFactory();
            else
                return new TraderFactory();
        }
    }
    }
    
    

Aucun commentaire:

Enregistrer un commentaire