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:
- 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.
- 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.
- 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
- 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
- Create an Abstract Factory Interface (e.g. IAbstractFactory) which defines the Abstract Object Interfaces created in step 1
- Ensure actual concrete object (e.g. Product11 , Product12, Product21 and Product22) implements one of the Abstract Object Interface as defined in step1
- 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 IAbstractProduct1End Interface''' <summary>
''' Abstract Product 2 (Group)
''' For example Here Abstract Product 2 Group is using AbtractProduct1
''' </summary>
Public Interface IAbstractProduct2Function Status(ByVal ab1 As IAbstractProduct1) As StringEnd 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 IAbstractProduct1Function CreateABF2() As IAbstractProduct2End Interface
Step 3.
''' <summary>
''' Product11 is part of AbstractProduct1 Group
''' </summary>
''' <remarks></remarks>
Public Class Product11Implements IAbstractProduct1
End ClassPublic Class Product12Implements IAbstractProduct1
End ClassPublic Class Product21Implements IAbstractProduct2
Public Function SendStatus(ab1 As IAbstractProduct1) As String Implements IAbstractProduct2.StatusDim msgString As String = Me.GetType().Name & " is using " & CType(ab1, Object).GetType().NameReturn msgString
End FunctionEnd ClassPublic Class Product22Implements IAbstractProduct2
Public Function SendStatus(ab1 As IAbstractProduct1) As String Implements IAbstractProduct2.StatusDim msgString As String = Me.GetType().Name & " is using " & CType(ab1, Object).GetType().NameReturn msgString
End FunctionEnd 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 ConcreteFactory1Implements IAbstractFactory
Public Function CreateABF1() As IAbstractProduct1 Implements IAbstractFactory.CreateABF1Return New Product11()End FunctionPublic Function CreateABF2() As IAbstractProduct2 Implements IAbstractFactory.CreateABF2Return New Product21()End FunctionEnd 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 ConcreteFactory2Implements IAbstractFactory
Public Function CreateABF1() As IAbstractProduct1 Implements IAbstractFactory.CreateABF1Return New Product12End FunctionPublic Function CreateABF2() As IAbstractProduct2 Implements IAbstractFactory.CreateABF2Return New Product22End FunctionEnd 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 ConcreteFactory2Dim prod1 = factory.CreateABF1
Dim prod2 = factory.CreateABF2
MsgBox(prod2.Status(prod1))