Real Time Implementation of Factory Methods

Introduction

Factory pattern deals with the instantiation of object without exposing the instantiation logic. In other words, a Factory is actually a creator of objects which have common interface.

It will increase the performance of your application is rapidly increased and the cost of the application is free fall.

Example

I’m having many oracle packages, each has many stored procs. Here some of the in parameters are similar to all the packages and also to the stored procs.



In the above diagram shows that each packages has many stored proc’s and each stored proc has many entities.

Here each stored proc has its own in and out parameters but some of the parameters are same for all the stored procs. Now take the common parameters out and create base class with those common entities.
We can implement the above scenario with the below approach.

  1. using System;  
  2.     /* 
  3.     * In Factory pattern, we create object without exposing the creation logic.  
  4.     * In this pattern, an interface is used for creating an object, 
  5.     * but let subclass decide which class to instantiate. 
  6.     * */  
  7. namespace FactoryMethod  
  8. {  
  9. Product (abstract)  
  10.     /* 
  11.     * Faza Class ITree 
  12.     * 
  13.     * */  
  14.     public interface ITree  
  15.     {  
  16.         string GetTreeName();  
  17.     }  
  18. Products (concrete)  
  19.     /* 
  20.     * The Concrete class which implements ITree 
  21.     *  
  22.     * */  
  23.   
  24.     public class BananaTree : ITree  
  25.     {  
  26.         public string GetTreeName()  
  27.         {  
  28.             return "My Name Is Banana Tree";  
  29.         }  
  30.     }  
  31.   
  32.     /* 
  33.     * The Concrete class which implements ITree 
  34.     *  
  35.     * */  
  36.     public class CoconutTree : ITree  
  37.     {  
  38.         public string GetTreeName()  
  39.         {  
  40.             return "My Name Is Coconut Tree";  
  41.         }  
  42.     }  
  43. Factory (abstract)  
  44.     /* 
  45.     * Faza Class TreeType  
  46.     * If you want you can add abstract class instad of faza class 
  47.     * 
  48.     * */  
  49.     public interface TreeType  
  50.     {  
  51.         ITree GetTree(string tree);  
  52.     }  
  53. Factory (concrete)  
  54.     /* 
  55.     * Concrete class which implements faza or concrete class 
  56.     * 
  57.     * */  
  58.     public class ConcreteTreeType : TreeType  
  59.     {  
  60.   
  61.         public ITree GetTree(string tree)  
  62.         {  
  63.   
  64.             if (tree == "COCONUT")  
  65.                 return new CoconutTree();  
  66.             else  
  67.                 return new BananaTree();  
  68.   
  69.         }  
  70.     }  
  71. Client code  
  72.     /* 
  73.     * main app. 
  74.     *  
  75.     *  */  
  76.     class Program  
  77.     {  
  78.         static void Main(string[] args)  
  79.         {  
  80.             TreeType oTreeType = new ConcreteTreeType();  
  81.   
  82.             ITree banana = oTreeType.GetTree("COCONUT");  
  83.             Console.WriteLine(banana.GetTreeName());  
  84.   
  85.             Console.ReadKey();  
  86.         }  
  87.     }}  
Conclusion

In Factory pattern, we create object without exposing the creation logic. In this pattern, an interface is used for creating an object but let subclass decide which class to instantiate.