Click here to Skip to main content
15,867,453 members
Articles / BUILD
Article

Abstract Factory

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 5.1K   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

This article was originally posted at http://wiki.asp.net/page.aspx/358/abstract-factory

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --