Friday, June 7, 2013

Flyweight Design Pattern in c#

 --> The flyweight design pattern allows you to reuse memory spaces in an application.
     --> when you have lots of objects that are almost identical in nature.
     -->  For example, if you are writing a game for a Smartphone
          where the amount of memory is very limited and you need to show many aliens that are identical in shape,
         *** you can have only one place that holds the shape of the alien instead of keeping each identical shape in the precious memory.
     --> In the flyweight pattern, there is the concept of
        1. Intrinsic 2.  Extrinsic state.
        1. Intrinsic state: Instrinstic states are things that are constant and are stored in memory.
        2. Extrinsic state: Extrinsic states are things that are not constant and need to be calculated on the fly, and are therefore not stored in the memory.
     *
     *
     * For example, in the game that we would like to create, the shapes of the aliens are all the same, but their color will change based on how mad each are. The shapes of the aliens will be Intrinsic, and the color of the alien will be Extrinsic
     
 class Program
    {
        static void Main(string[] args)
        {
            //create aliens and store in factory.
            AlienFactory factory = new AlienFactory();
            factory.SaveAlien(0,new LargeAlign());
            factory.SaveAlien(1, new LittleAlien());

            //now access the flyweight objects
            IAlien a = factory.GetAlien(0);
            IAlien b = factory.GetAlien(1);

            //show intrinsic states, all accessed in memory without calculations
            Console.WriteLine("Showing intrinsic states...");
            Console.WriteLine("Alien of type 0 is " + a.Shape);
            Console.WriteLine("Alien of type 1 is " + b.Shape);

            //show extrinsic states, need calculations
            Console.WriteLine("Showing extrinsic states...");
            Console.WriteLine("Alien of type 0 is " + a.GetColor(0).ToString());
            Console.WriteLine("Alien of type 0 is " + a.GetColor(1).ToString());
            Console.WriteLine("Alien of type 1 is " + b.GetColor(0).ToString());
            Console.WriteLine("Alien of type 1 is " + b.GetColor(1).ToString());

            Console.ReadKey();
        }
    }

    //===Flyweight Factory ===
    public class AlienFactory
    {
        private Dictionary<int, IAlien> list = new Dictionary<int, IAlien>();
        public void SaveAlien(int index, IAlien alien)
        {
            list.Add(index, alien);
        }
        public IAlien GetAlien(int index)
        {
            return list[index];
        }
    }


    //===Interface===
    public enum Color { Green, Red, Blue }
    public interface IAlien
    {
        string Shape { get; } //intrinsic state
        Color GetColor(int madLevel); //extrinsic state
    }

    //===Impement interface===
    public class LargeAlign : IAlien
    {
        private string _shape = "Large Shape";//intrinsic state
        public string Shape
        {
            get { return _shape; }
        }

        public Color GetColor(int madLevel)//extrinsic state
        {
            if (madLevel == 0)
                return Color.Red;
            else if (madLevel == 1)
                return Color.Green;
            else
                return Color.Blue;
        }

    }
    public class LittleAlien : IAlien
    {
        private string _shape = "Little Shape";//intrinsic state
        public string Shape
        {
            get { return _shape; }
        }

        public Color GetColor(int madLevel)//extrinsic state
        {
            if (madLevel == 0)
                return Color.Red;
            else if (madLevel == 1)
                return Color.Green;
            else return Color.Blue;
        }

    }

No comments:

Post a Comment