Home / ASP.NET Wiki / Architecture / Design Patterns / Factory / Abstract Factory

Abstract Factory

 Rate It (12)

Abstract Factory

The Gang of Four defintion for this design pattern is: "Provide an interface for creating families of related or dependant objects without specifying their concrete classes.".

A VB example of the Abstract Factory Design Pattern

' This code could be run at the page behind level
Dim
aCarFactory As CarFactory

' Ideally we would use the Factory Design pattern
' but I omitted this for simplicity
If aVariable = "Ford" Then
    
aCarFactory = New FordCarFactory
Else
    
aCarFactory = New VauxhallCarFactory
End If

' Build 2 family cars
Dim aFamilyCar1 As ICar = aCarFactory.CreateFamilyCar
Dim aFamilyCar2 As ICar = aCarFactory.CreateFamilyCar

' Build a City Car
Dim aCityCar As ICar = aCarFactory.CreateCityCar

' The base class Car Factory
Public MustInherit Class CarFactory
   
Public MustOverride Function CreateFamilyCar() As ICar

    Public MustOverride Function CreateCityCar() As ICar
End Class

' The concrete Ford car factory
Public Class FordCarFactory
   
Inherits CarFactory

    Public Overrides Function CreateCityCar() As ICar
        
Return New FordCityCar
   
End Function

    Public Overrides Function CreateFamilyCar() As ICar
        
Return New FordFamilyCar
   
End Function
End
Class

' The concrete Vauxhall car factory
Public Class VauxhallCarFactory
   
Inherits CarFactory

    Public Overrides Function CreateCityCar() As ICar
       
Return New VauxhallCityCar
    
End Function

    Public Overrides Function CreateFamilyCar() As ICar
       
Return New VauxhallFamilyCar
   
End Function
End
Class

' The Car Interface, all cars must implement these methods
Public Interface ICar
 
   
Sub Drive()    

    Function
Desc() As String

End
Interface

' The concrete Ford Family Car
Public Class FordFamilyCar
   
Implements ICar

    Public Sub Drive() Implements ICar.Drive
      
' Drive car
   
End Sub

    Public Function Desc() As String Implements ICar.Desc
      
Return "Ford Family Car seats 8"
   
End Function
End
Class

' The concrete Ford City Car
Public Class FordCityCar
   
Implements ICar

    Public Sub Drive() Implements ICar.Drive
      
' Drive car
   
End Sub

    Public Function Desc() As String Implements ICar.Desc
      
Return "Ford Ciy Car seats 2"
   
End Function
End
Class

' The concrete Vauxhall City Car
Public Class VauxhallCityCar
   
Implements ICar

    Public Sub Drive() Implements ICar.Drive
      
' Drive car
   
End Sub

    Public Function Desc() As String Implements ICar.Desc
       
Return "Vauxhall Ciy Car seats 4"
   
End Function
End
Class

' The concrete Vauxhall Family Car
Public Class VauxhallFamilyCar
   
Implements ICar

    Public Sub Drive() Implements ICar.Drive
       
' Drive car
   
End Sub

    Public Function Desc() As String Implements ICar.Desc
       
Return "Vauxhall Family Car seats 6"
   
End Function
End
Class

External links

Revision number 5, Wednesday, September 23, 2009 5:01:24 PM by TheNephalim

Comments

Hi, Thanks for sharing. I wrote about the Abstract Factory pattern in my blog in more details and provided a C# code. You can read about it in the following link: http://blogs.microsoft.co.il/blogs/gilf/archive/2008/05/03/abstract-factory-pattern.aspx

I think this sample somewhat misquote Abstract Factory. In this sample, CarFactory can be designed to produce both Ford and Vauxhall (remove FordCarFactory and VauxhallCarFactory), that'll be simple and better. For abstract factory pattern, I would use following sample in real world: MouseFactory creates Mouse objects, KeyBoardFactory creates KeyBoard objects, PrinterFactory creates Printer objects, (above three are Factory pattern) and there is an absctract factory, whose name is PeripheralFactory, creates peripheral objects, which contain mouse, keyboard, and printer fields. PeripheralFactory is an abstract class, which should be inherited by Concrete PeripheralFactory, such as MSPeripheralFactory, HPPeripheralFactory, and IBMPeripheralFactory. All concrete PeripheralFactory will invoke corresponding creating methods of MouseFactory, KeyBoardFactory, and PrinterFacotry to create a Peripheral object.

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)