Abstract Factory and Proxy design patterns

Abstract Factory

I have many individual types and those types will be used by some other types. For the consumer types, it has to know about all the individual types, so instead of doing this, we can create a medium by which consumer can access all the individual types. For example, I have typeA, typeB and typeC, so instead of consumer create objects of all those types, if we used Abstract Factory pattern then we can used a new factory types with facility like CreateTypeA(),  CreateTypeB(), CreateTypeC() method which will return relevant objects. For details we will go through the example below,

Following class diagram will give the picture of the abstract factory pattern example project.


Fig: Abstract Factory Pattern class diagram.

and the project structure in Visual studio,



Fig: Project structure of the Abstract Factory design pattern.
Source code of the projects:

Factory class of AbstractFactory project - this class exposes two methods to the consumers.

namespace AbstractFacotry
{
    using BaseClassDefinition;
    using BaseClassImplementation;
 
    public class Factory
    {
        public ProductABaseClass CreateProductA()
        {
            return new ProductA();
        }
        public ProductBBaseClass CreateProductB()
        {
            return new ProductB();
        }
    }
}


ProductABaseClass and ProductBBaseClass of BaseClassDefinition - following classes define the definition of the concrete class.

namespace BaseClassDefinition
{

    public abstract class ProductABaseClass
    {
        public abstract int GetProductAPrice();
    }
}
namespace BaseClassDefinition
{
    public abstract class ProductBBaseClass
    {
        public abstract int GetProductBPrice();
    }
}


ProductA and ProductB of BaseClassImplementation project - ProductA and ProductB class is the implementation of the BaseClassImplementation.

namespace BaseClassImplementation
{
    using BaseClassDefinition;
    public class ProductA : ProductABaseClass
    {
 
        public override int GetProductAPrice()
        {
            return 1;
        }
    }
}

namespace BaseClassImplementation
{
    using BaseClassDefinition;
    public class ProductB : ProductBBaseClass
    {
        public override int GetProductBPrice()
        {
            return 2;
        }
    }
}


Program class of TestHarness project - this test harness will use the Factory class to instantiate the concrete classes for example in ProductA and ProductB via Factory class.

namespace TestHarness
{
    using System;
    using AbstractFacotry;
    using BaseClassDefinition;
 
    class Program
    {
        static void Main(string[] args)
        {
            ProductABaseClass childOneOfBaseClass = new Factory().CreateProductA();
            ProductBBaseClass childTwoOfBaseClass = new Factory().CreateProductB();
 
            Console.WriteLine("{0}", childOneOfBaseClass.GetProductAPrice());
            Console.WriteLine("{0}", childTwoOfBaseClass.GetProductBPrice());
        }
    }
}
 

Output of the above Test Harness program is as below,

1
2
Press any key to continue . . .


Proxy Pattern

It provides a way to access real object via another types. If we see the Service References of WCF application then we will find a real example of Proxy Pattern. 

See the below class diagram for Proxy Pattern project,


Fig: Class diagram of Proxy pattern project.
and  project structure of the proxy pattern



Fig: Project structure of the Proxy Pattern
AbstractProduct class of ProxyPattern-BaseClassDefiition project - base type of all the children.

namespace ProxyPattern_BaseClassDefinition
{
    public abstract class AbstractProduct
    {
        public abstract string GetProductName();
    }
}


ProductA class of ProxyPattern-BaseClassImplementation - Implementation of base class.

namespace ProxyPattern_BaseClassImplementation
{
    using ProxyPattern_BaseClassDefinition;
    public class ProductA : AbstractProduct
    {
        public override string GetProductName()
        {
            return "Product A";
        }
    }
}


Proxy_ProductA class of ProxyPattern-Proxy project - a proxy of ProductA class.

namespace ProxyPattern_Proxy
{
    using ProxyPattern_BaseClassDefinition;
    using ProxyPattern_BaseClassImplementation;
    public class Proxy_ProductA : AbstractProduct
    {
        private ProductA productA;
        public override string GetProductName()
        {
            if (productA == null)
                productA = new ProductA();
            return productA.GetProductName();
        }
    }
}


Program class of ProxyPattern-TestHarness where I use Proxy class Proxy_ProductA to access the real object which is ProductA.

namespace ProxyPattern_TestHarness
{
    using System;
    using ProxyPattern_Proxy;
    class Program
    {
        static void Main(string[] args)
        {
            Proxy_ProductA proxy = new Proxy_ProductA();
            Console.WriteLine("{0}", proxy.GetProductName());
        }
    }
}


The output of the Test harness is as below,

Product A
Press any key to continue . . .


Note:

always happy to learn from others and happy to get new ideas. thanks. mohammad

 Few C# and Application Design books from Amazon,

3 comments:

  1. Great Explanation. Another great article i recommend is this one

    ReplyDelete
  2. thanks for this information .
    you can find factory design pattern in simple and easy to understand language in a detail on
    http://www.itsoftpoint.com/?page_id=2666

    ReplyDelete