Factory

 Rate It (3)

Factory

The job of the Factory design pattern is to create concrete sub classes. You can see the Factory desing pattern used throughout the .NET Framework.

A VB example of the Factory Pattern

' A Simple Interface

Public Interface IVehicle

    Sub Drive(ByVal miles As Integer)

 

End Interface

 

' The Vehicle Factory

Public Class VehicleFactory

    Public Shared Function getVehicle(ByVal Vehicle As String) As IVehicle

 

        Select Case Vehicle

            Case "Car"

                Return New Car

            Case "Lorry"

                Return New Lorry

            Case Else

                Throw New ApplicationException(String.Format("Vehicle '{0}' cannot be created", Vehicle))

        End Select

 

    End Function

End Class

 

' A Car Class that Implements the IVehicle Interface

Public Class Car

    Implements IVehicle

    Public Sub Drive(ByVal miles As Integer) Implements IVehicle.Drive

        ' Drive the Car

    End Sub

End Class

 

' A Lorry Class that Implements the IVehicle Interface

Public Class Lorry

    Implements IVehicle

    Public Sub Drive(ByVal miles As Integer) Implements IVehicle.Drive

        ' Drive the Lorry

    End Sub

End Class

UML

Factory Design Pattern

Articles

 

Revision number 5, Saturday, February 23, 2008 6:55:48 AM by scott@elbandit.co.uk

Comments

Featured ASP.NET Web Hosting

Powerful, Award Winning ASP.NET Hosting Trusted By Over 30,000 ASP.NET Developers

3 Months Free & No Setup Fees – CLICK HERE!

Table of Contents

Top Wiki Contributors

Advertise Here