Abstract Factory





0/5 (0 vote)
Abstract FactoryThe Gang of Four defintion for this design pattern is: "Provide an interface for creating families of related or dependant objects
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
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
End Class
' The concrete Ford car factory
Public Class FordCarFactory
Inherits CarFactory
Public Overrides Function CreateCityCar() As ICar
Return New FordCityCar
End Function
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
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
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
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
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