Dofactory.com
Dofactory.com
 Back to list
Views:   14.4K
Replies:  1
Archived

Abstract Factory Pattern With Generic Usage

 Hi ,

   I want to use abstract factory pattern with generics. So i wrote a generic gauge factory which is below. I have "HalfGauge" and "QuarterGauge" abstract classes. I have concrete classes  'TotalHalfGauge','NewHalfGauge','FrequencyHalfGauges' ,'TotalQuarterGauge','NewQuarterGauge','FrequencyQuarterGauges which inherites HalfGauge or QuarterGauges.

 
       public class GenericGaugeFactory<T, K> : IGenericGaugeFactory<T, K>
        where T : HalfGauge, new()
        where K : QuarterGauge, new()
         {
 
                private static GenericGaugeFactory<T, K> _instance;
                private static object lockObject = new object();
                public static GenericGaugeFactory<T, K> Instance
                {
                    get
               {
                if (_instance == null)
                {
                    lock (lockObject)
                    {
                        if (_instance == null)
                            _instance = new GenericGaugeFactory<T, K>();
                    }
                }
                return _instance;
            }
            set { _instance = value; }
        }


        public GenericGaugeFactory() { }

        public T CreateHalfGauge()
        {
            return new T();
        }
        public K CreateQuarterGauge()
        {
            return new K();
        }
    }

  
Now in my client i want to access my GenericGaugeFactory class  like below which takes a GaugeKpiTypes.In switch case blocks i always declare new  variables.I dont want this.

I only declare a common variable like below but it gives error. How can i achive this

       IGenericGaugeFactory<HalfGauge, QuarterGauge> _gaugeFactory2 = null;

        switch (GaugeKpis)
        {
            case GaugeKpiEnums.Total: { _gaugeFactory2 = new GenericGaugeFactory<TotalHalfGauge, TotalQuarterGauge>(); break; }
            case GaugeKpiEnums.New: { _gaugeFactory2 = new GenericGaugeFactory<NewHalfGauge, NewQuarterGauge>(); break; }
            case GaugeKpiEnums.Frequency: { _gaugeFactory2 = new GenericGaugeFactory<FrequencyHalfGauge, FrequencyQuarterGauge>(); break; }
            case GaugeKpiEnums.Unique: { _gaugeFactory2 = new GenericGaugeFactory<UniqueHalfGauge, UniqueQuarterGauge>(); break; }
            case GaugeKpiEnums.UniqueTotal: { _gaugeFactory2 = new GenericGaugeFactory<UniqueTotalHalfGauge, UniqueTotalQuarterGauge>(); break; }
            case GaugeKpiEnums.Dwell: { _gaugeFactory2 = new GenericGaugeFactory<DwellTimeHalfGauge, DwellTimeQuarterGauge>(); break; }
            case GaugeKpiEnums.Retention: { _gaugeFactory2 = new GenericGaugeFactory<RetentiontimeHalfGauge, RetentiontimeQuarterGauge>(); break; }
        }


Thanks For your helps.

Best Regards.



Volkan Genç, Dec 03, 2012
Reply 1
Hi,

  I Can do it with Covarience like below sample. I want to handle common variable.

     public class Program
    {
        static void Main(string[] args)
        {

        

            IGenericFactory<IAnimal> hayvan = new GenericFactory<Cat>();
            hayvan = new GenericFactory<Dog>();

            IAnimal cat = hayvan2.Create();
            cat.Speak();
            Console.ReadLine();


        }
    }



   public interface IAnimal
    {
        void Speak();
    }

    public class Cat : IAnimal
    {
        public void Speak()
        {
            Console.WriteLine("Miyaww");
        }
    }

    public class Dog : IAnimal
    {
        public void Speak()
        {
            Console.WriteLine("Hqw haww");
        }
    }




    public interface IGenericFactory<out T>
    {
        T Create();
    }

    public class GenericFactory<T> : IGenericFactory<T> where T : IAnimal, new()
    {

        public GenericFactory() { }

        public T Create()
        {
            return new T();
        }

    }

Best Regards.
Volkan Genç, Apr 18, 2013
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.