Design Patterns - Part 1 Abstract Design Pattern

Hello,

Today I'm going to explain about something important to all the programmers....hmmm Design patterns...sound familiar? okay before going to the code level let's discuss about these design patterns. so basically design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects.
           The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral. okay I'm not going to tell much and much boring stuff here. so let's go and learn design pattern one by one. Today I'm going to demonstrate about Abstract Factory Pattern

Definition :
Provide an interface for creating families of related or dependent objects without specifying their concrete classes. here is the sample code in c#


Main Class



   /// <summary>  
   /// Main Class   
   /// </summary>  
   class Program  
   {  
     /// <summary>  
     /// Main Method of the Application  
     /// </summary>  
     /// <param name="args"></param>  
     static void Main(string[] args)  
     {  
       //Abstract Factory Number 1  
       AbstractFactory factoryOne = new ConcreteFactoryOne();  
       Client clientOne = new Client(factoryOne);  
       clientOne.Run();  
   
       //Abstract Factory Number 2  
       AbstractFactory factoryTwo = new ConcreteFactoryTwo();  
       Client clientTwo = new Client(factoryTwo);  
       clientTwo.Run();  
   
       //Waiting for user input  
       Console.ReadLine();  
     }  
   }  

Factory Classes

   /// <summary>  
   /// Abstract Factory Class  
   /// </summary>  
   public abstract class AbstractFactory  
   {  
     public abstract AbstractProductOne CreateProductOne();  
     public abstract AbstractProductTwo CreateProductTwo();  
   }  
   
   /// <summary>  
   /// Concrete Factory One Class  
   /// </summary>  
   public class ConcreteFactoryOne : AbstractFactory  
   {  
     /// <summary>  
     /// Create Product One Instance  
     /// </summary>  
     /// <returns>Product One Object</returns>  
     public override AbstractProductOne CreateProductOne()  
     {  
       return new ProductOneA();  
     }  
   
     /// <summary>  
     /// Create Product Two Instance  
     /// </summary>  
     /// <returns>Product Two Object</returns>  
     public override AbstractProductTwo CreateProductTwo()  
     {  
       return new ProductTwoA();  
     }  
   }  
   
   /// <summary>  
   /// Concrete Factory Two Class  
   /// </summary>  
   public class ConcreteFactoryTwo : AbstractFactory  
   {  
     /// <summary>  
     /// Create Product One Instance  
     /// </summary>  
     /// <returns>Product One Object</returns>  
     public override AbstractProductOne CreateProductOne()  
     {  
       return new ProductOneB();  
     }  
   
     /// <summary>  
     /// Create Product Two Instance  
     /// </summary>  
     /// <returns>ProductTwoB Object</returns>  
     public override AbstractProductTwo CreateProductTwo()  
     {  
       return new ProductTwoB();  
     }  
   }  

Product Classes

   /// <summary>  
   /// Abstract Factory Class  
   /// </summary>  
   public abstract class AbstractFactory  
   {  
     public abstract AbstractProductOne CreateProductOne();  
     public abstract AbstractProductTwo CreateProductTwo();  
   }  
   
   /// <summary>  
   /// Concrete Factory One Class  
   /// </summary>  
   public class ConcreteFactoryOne : AbstractFactory  
   {  
     /// <summary>  
     /// Create Product One Instance  
     /// </summary>  
     /// <returns>Product One Object</returns>  
     public override AbstractProductOne CreateProductOne()  
     {  
       return new ProductOneA();  
     }  
   
     /// <summary>  
     /// Create Product Two Instance  
     /// </summary>  
     /// <returns>Product Two Object</returns>  
     public override AbstractProductTwo CreateProductTwo()  
     {  
       return new ProductTwoA();  
     }  
   }  
   
   /// <summary>  
   /// Concrete Factory Two Class  
   /// </summary>  
   public class ConcreteFactoryTwo : AbstractFactory  
   {  
     /// <summary>  
     /// Create Product One Instance  
     /// </summary>  
     /// <returns>Product One Object</returns>  
     public override AbstractProductOne CreateProductOne()  
     {  
       return new ProductOneB();  
     }  
   
     /// <summary>  
     /// Create Product Two Instance  
     /// </summary>  
     /// <returns>ProductTwoB Object</returns>  
     public override AbstractProductTwo CreateProductTwo()  
     {  
       return new ProductTwoB();  
     }  
   }  
   
   /// <summary>  
   /// Abstract Product one Class  
   /// </summary>  
   public abstract class AbstractProductOne  
   {  
        
   }  
   
   /// <summary>  
   /// Abstract Product two Class  
   /// </summary>  
   public abstract class AbstractProductTwo  
   {  
     /// <summary>  
     /// Interaction Method  
     /// </summary>  
     /// <param name="productOne">Instance of AbstractProductOne Class</param>  
     public abstract void Interact(AbstractProductOne productOne);  
   }  
   
   /// <summary>  
   /// Product One Class  
   /// </summary>  
   public class ProductOneA : AbstractProductOne  
   {  
   
   }  
   /// <summary>  
   /// Product Two Class  
   /// </summary>  
   public class ProductTwoA : AbstractProductTwo  
   {  
     /// <summary>  
     /// Interaction Method  
     /// </summary>  
     /// <param name="productOne">Instance of AbstractProductOne Class</param>  
     public override void Interact(AbstractProductOne productOne)  
     {  
       Console.WriteLine(this.GetType().Name + " interacts with " + productOne.GetType().Name);  
     }  
   }  
   
   /// <summary>  
   /// Product One Class  
   /// </summary>  
   public class ProductOneB : AbstractProductOne  
   {  
   
   }  
   /// <summary>  
   /// Product Two Class  
   /// </summary>  
   public class ProductTwoB : AbstractProductTwo  
   {  
     /// <summary>  
     /// Interaction Method  
     /// </summary>  
     /// <param name="productOne">Instance of AbstractProductOne Class</param>  
     public override void Interact(AbstractProductOne productOne)  
     {  
       Console.WriteLine(this.GetType().Name + " interacts with " + productOne.GetType().Name);  
     }  
   }  

Client Class

  /// <summary>  
   /// Client Class : Class which going to interact with the Products  
   /// </summary>  
   public class Client  
   {  
     /// <summary>  
     /// Abstract Product One  
     /// </summary>  
     private readonly AbstractProductOne abstractProductOne;  
   
     /// <summary>  
     /// Abstract Product Two  
     /// </summary>  
     private readonly AbstractProductTwo abstractProductTwo;  
   
   
      
     /// <summary>  
     /// Constructor  
     /// </summary>  
     /// <param name="factory">Instance of Abstract Factory Class</param>  
     public Client(AbstractFactory factory)  
     {  
       abstractProductOne = factory.CreateProductOne();  
       abstractProductTwo = factory.CreateProductTwo();  
     }  
   
     public void Run()  
     {  
       abstractProductTwo.Interact(abstractProductOne);  
     }  
   }  

Final Output




Hmmm Long post right? so now I hope you will get some idea about this Abstract Factory Pattern and if you want to learn more about this pattern please refer this. from next design pattern post I'm going to tell about the Builder Pattern. See you soon.... bye bye...


Comments

Popular posts from this blog

Deep dive into OpenXML Part 1

Deep dive into OpenXML Part 2

Convert XML data to dynamic (ExpandoObject) Object