Click here to Skip to main content
Click here to Skip to main content
Go to top

Factory

, 20 Oct 2013
Rate this:
Please Sign up or sign in to vote.
FactoryThe job of the Factory design pattern is to create concrete sub classes. You can see the Factory design pattern used throughout the .NET

Editorial Note

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.

Introduction

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

The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses."
Factory methods encapsulate the creation of objects. This can be useful if the creation process is very complex, for example if it depends on settings in configuration files or on user input.

A C# and VB.NET example of the Factory Pattern

// A Simple Interface
public interface IVehicle
{
    void Drive(int miles);
}

// The Vehicle Factory
public class VehicleFactory
{
    public static IVehicle getVehicle(string Vehicle)
    {
        switch (Vehicle) {
            case "Car":
                return new Car();

            case "Lorry":
                return new Lorry();

            default:
                throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Vehicle));
                break;
        }
    }
}

// A Car Class that Implements the IVehicle Interface
public class Car : IVehicle
{
    public void IVehicle.Drive(int miles)
    {
        // Drive the Car
    }
}

// A Lorry Class that Implements the IVehicle Interface
public class Lorry : IVehicle
{
    public void IVehicle.Drive(int miles)
    {
        // Drive the Lorry
    }
} 
' 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 Diagram

Factory Design Pattern

Articles 

License

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

Share

About the Author

ASP.NET Community

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.
Group type: Collaborative Group

247 members


Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 20 Oct 2013
Article Copyright 2008 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid