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 simplicityIf aVariable = "Ford" Then
     
aCarFactory = New FordCarFactoryElse
     
aCarFactory = New VauxhallCarFactoryEnd If
' Build 2 family carsDim aFamilyCar1 As ICar = aCarFactory.CreateFamilyCarDim aFamilyCar2 As ICar = aCarFactory.CreateFamilyCar
' Build a City CarDim aCityCar As ICar = aCarFactory.CreateCityCar
' The base class Car FactoryPublic MustInherit Class CarFactory
    
Public MustOverride Function CreateFamilyCar() As ICar
    Public MustOverride Function CreateCityCar() As ICarEnd Class
' The concrete Ford car factoryPublic 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 factoryPublic 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 methodsPublic Interface ICar

    
Sub Drive()    

    Function
 Desc() As String

End
 Interface
' The concrete Ford Family CarPublic 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 CarPublic 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 CarPublic 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 CarPublic 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