Archives

All posts for the year 2019

Abstract Factory provide common interface for a group of similar classes, without creating concrete class.

public interface IClass{
          public string GetMethodResult();
} 
public class ClassA : IClass{
         public string GetMethodResult(){
                return "Result from Class A";
         }
}

public class ClassB : IClass {
        public string GetMethodResult(){
               return "Result from Class B";
        }
}

public interface IFactory{
          public IClass CreateClass();
}

public class ClassFactoryA : IFactory{
         public IClass CreateClass(){
                return new ClassA();
         }
}


public class ClassFactoryB : IFactory {
        public IClass CreateClass(){
               return new ClassB();
        }
}

public enum ClassType {
       Class_A, Class_B
} 

void Main{
       IFactory factory;
       switch(type){
           case type.Class_A:
                factory = new ClassFactoryA();
           case type.Class_B:
                factory = new ClassFactoryB();
        }
       var aClass = factory.CreateClass();
       var result = aClass.GetMethodResult();
        
} 

Define an interface for creating an object, but let sub-classes decide which class to instantiate. Factory Method lets a class defer instantiation to sub-classes.

That means, You can create a object without specifying exact class, by using /calling factory method.

public interface ISubClass{
          public string GetMethodResult();
} 


public class ClassA : ISubClass {
         public string GetMethodResult(){
                return "Result from Class A";
         }
}


public class ClassB : ISubClass {
        public string GetMethodResult(){
               return "Result from Class B";
        }
}

public enum ClassType {
       Class_A, Class_B
} 

public class ClassFactory{
       public ISubClass GetClass(ClassType type){
               switch(type){
                    case type.Class_A:
                             return new ClassA();
                    case type.Class_B:
                             return new ClassB();
                }
        }
} 

New interesting blog with stunning stories.

http://www.tijanapavlovic.com

The Real-Time Terminal for Investment Advisors, Traders, Analysts and Fund Managers

webstation.teletrader.com