Vishwamohan

Welcome to Vishwa's blog - Technology, Spirituality and More...

Abstract Factory Design Pattern

It is one of the highly used Creational Pattern from Gang of Four patterns. It provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the concrete objects that are part of the theme.

 

The client does not know (or care) which concrete objects it gets from each of these internal factories since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage.

 

When and where to use it

You may wonder why you want to create objects using another class (called Abstract Factory) rather than calling constructors directly. Here are some reasons:

  1. Constructors are limited in their control over the overall creation process. If an application needs more control, using Factory is best option. E.g. sharing and re-using objects like object caching, or applications that maintain object and type counts.
  2. There are times when the client does not know exactly what type to construct to use. It is easier to code against a base type or an interface and then let a factory make the decision. ADO.NET objects such as DBConnection, DBCommand etc. are the examples.
  3. Constructors don’t communicate their intention very well because they must be named after their class. Having numerous overloaded constructors may make it hard for the developer to decide which constructor to use. Replacing constructors with intention-revealing creation methods are frequently preferred. 

 

Steps to Create Abstract Factory

  1. First Create Abstract Objects Interfaces (e.g. IAbstractProduct1, IAbstractProduct2) – It is like making of group of objects which will implement the set of same interfaces
  2. Create an Abstract Factory Interface (e.g. IAbstractFactory) which defines the Abstract Object Interfaces created in step 1
  3. Ensure actual concrete object (e.g. Product11 , Product12, Product21 and Product22) implements one of the Abstract Object Interface as defined in step1
  4. Finally Create Concrete Factories(e.g. ConcreteFactory1, ConcreteFactory2) which implements Abstract Factory Interface defined in step 2

 

Sample Code

Step 1.

''' <summary>
''' Abstract Product 1 (Group)
''' </summary>
''' <remarks></remarks>
Public Interface IAbstractProduct1
 
End Interface
 
''' <summary>
''' Abstract Product 2 (Group)
''' For example Here Abstract Product 2 Group is using AbtractProduct1 
''' </summary>
Public Interface IAbstractProduct2
    Function Status(ByVal ab1 As IAbstractProduct1) As String
End Interface

 

Step 2.

''' <summary>
''' Highest Level of Abstraction
''' The Abstract Factory
''' It defines two Abstract Product Groups (1 and 2)
''' </summary>
''' <remarks></remarks>
Interface IAbstractFactory
    Function CreateABF1() As IAbstractProduct1
    Function CreateABF2() As IAbstractProduct2
End Interface

 

Step 3.

''' <summary>
''' Product11 is part of AbstractProduct1 Group
''' </summary>
''' <remarks></remarks>
 
Public Class Product11
    Implements IAbstractProduct1
 
End Class
 
Public Class Product12
    Implements IAbstractProduct1
 
End Class
 
Public Class Product21
    Implements IAbstractProduct2
 
    Public Function SendStatus(ab1 As IAbstractProduct1) As String Implements IAbstractProduct2.Status
        Dim msgString As String = Me.GetType().Name & " is using " & CType(ab1, Object).GetType().Name
        Return msgString
    End Function
End Class
 
Public Class Product22
    Implements IAbstractProduct2
 
    Public Function SendStatus(ab1 As IAbstractProduct1) As String Implements IAbstractProduct2.Status
        Dim msgString As String = Me.GetType().Name & " is using " & CType(ab1, Object).GetType().Name
        Return msgString
    End Function
End Class

 

Step 4

''' <summary>
''' Abstract Factory gets implemented using the Concrete Factory
''' These methods finally gets implemented to return the respective product or object
''' </summary>
''' <remarks></remarks>
Public Class ConcreteFactory1
    Implements IAbstractFactory
 
    Public Function CreateABF1() As IAbstractProduct1 Implements IAbstractFactory.CreateABF1
        Return New Product11()
    End Function
 
    Public Function CreateABF2() As IAbstractProduct2 Implements IAbstractFactory.CreateABF2
        Return New Product21()
    End Function
End Class
 
''' <summary>
''' Abstract Factory gets implemented using the Concrete Factory
''' These methods finally gets implemented to return the respective product or object
''' </summary>
''' <remarks></remarks>
 
Public Class ConcreteFactory2
    Implements IAbstractFactory
 
    Public Function CreateABF1() As IAbstractProduct1 Implements IAbstractFactory.CreateABF1
        Return New Product12
    End Function
 
    Public Function CreateABF2() As IAbstractProduct2 Implements IAbstractFactory.CreateABF2
        Return New Product22
    End Function
End Class

 

Client to Test this Design Pattern

'You can use either ConcreateFactory for this test - Using a Windows app client
        'Dim factory As New ConcreteFactory1
        Dim factory As New ConcreteFactory2
        Dim prod1 = factory.CreateABF1
        Dim prod2 = factory.CreateABF2
        MsgBox(prod2.Status(prod1))

Comments (1) -

  • Vaidehi

    4/25/2012 11:20:05 AM | Reply

    Comments on your thread for http error 50019 are closed, I am facing same problem and web.config file is missing. what should I do?

Loading