Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Abstract Factory

0.00/5 (No votes)
23 Sep 2009 1  
Abstract FactoryThe Gang of Four defintion for this design pattern is: Provide an interface for creating families of related or dependant objects

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here