luni, 25 ianuarie 2010

Factory Method Pattern

Factory Method Pattern

  • is a way of creating objects, but letting subclasses decide exactly which class to instantiate.
  • instantiates the proper class/es based on current state/info provided by client
  • hide the class name from the place where an object is instantiated




  • CreateInstance()  - factory method which decides which class to be instantiated
  • IResult - defines the interface for the object that factory method creates
  • Result - concreate implementation of IResult
  • Creator - declares the factory method, which returns an object of type IResult
  • Client - class that use Creator to instantiate classes that implement IResult

Sample:


Client       StatisticsApp
Creator    StatisticsModule
IResult     IView
Result_1   ChartView
Result_2   GridView



using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Statistics
{
    //Type of IView to be created
    public enum ViewType
    {
        kChart,
        kGrid
    }

    interface IView
    {
        String Name { get; }
        void RegisterStats(IEnumerable<String> _statNames);
        void DeregisterStats();
        void OpenView();
        void CloseView();
    }

    //base implementation for for all View classes
    public abstract class BaseView : IView
    {
        protected String _name;
        protected List<String> _stats;
        public BaseView(string i_name)
        {
            _name = i_name;
            Console.WriteLine(GetType() + " view created!");
        }

        public String Name
        {
            get { return _name; }
        }

        public virtual void RegisterStats(IEnumerable<String> _statNames) { _stats = new List<String>(_statNames); }
        public virtual void DeregisterStats() { if (_stats != null) _stats.Clear(); }
        public virtual void OpenView()
        {
            StringBuilder _sb = new StringBuilder();
            _sb.AppendLine("Statistics in view: " + _name);
            foreach (string _stat in _stats)
            {
                _sb.AppendLine("\t" + _stat);
            }
            Console.WriteLine(_sb.ToString());
        }

        public abstract void CloseView();
    }

    public class ChartView : BaseView
    {
        public ChartView(String _name) : base(_name) { }
        public override void OpenView()
        {
            base.OpenView();
            Console.WriteLine("Open ChartView: " + _name);
        }

        public override void CloseView() {
            Console.WriteLine("Close ChartView: " + _name); }
    }

    public class GridView : BaseView
    {
        public GridView(String _name) : base(_name) { }
        public override void OpenView()
        {
            base.OpenView();
            Console.WriteLine("Open GridView: " + _name);
        }

        public override void CloseView()
        {
            Console.WriteLine("Close GridView: " + _name);
        }
    }

    class StatisticsModule
    {
        public static IView CreateInstance(String i_name, ViewType i_viewType)
        {
            switch (i_viewType)
            {
                case ViewType.kChart:
                    return new ChartView(i_name);
                case ViewType.kGrid:
                    return new GridView(i_name);
                default:
                    throw new NotSupportedException("Not supported view type");
            }
        }
    }

    class StatisticsApp
    {
        readonly Queue<IView> _views = new Queue<IView>(4);
        internal void Run()
        {
            for (int i = 0; i < 12; i++)
            {
                IView _newView = StatisticsModule.CreateInstance("View " + i, i % 2 == 0 ? ViewType.kGrid : ViewType.kChart);
                _newView.RegisterStats(new List<String>(new String[] { "Stat 1", "Stat 2" }));
                if (_views.Count == 4)
                {
                    IView _view = _views.Dequeue();
                    _view.DeregisterStats();
                    _view.CloseView();
                }
                _views.Enqueue(_newView);
                _newView.OpenView();
                Thread.Sleep(2000);
            }
            Console.WriteLine("Opened View are: ");
            {
                foreach (IView view in _views)
                {
                    Console.WriteLine(view.Name + view.GetType());
                }
            }
        }
        static void Main()
        {
            StatisticsApp _statsApp = new StatisticsApp();
            _statsApp.Run();
            Console.ReadLine();
        }
    }
}



Niciun comentariu:

Trimiteți un comentariu