Wednesday, November 12, 2003

Software Design Patterns


Partly for my own education and partly to interest anyone who may be reading this blog, I'm going to do a series of posts on software design patterns. I find that describing something so another can understand it improves my own understanding, so I'll begin with the "Gang of Four" Factory Pattern.

Factory Pattern


Basic Description

A Factory is a class (or it can be just a method) that creates objects derived from a common super-class or interface. It is usually used in a situation where you don't want your client class to have to figure out what the specific class of an object is, because it could be of several different types. You create a super-class or interface and code the client to call methods of that super-class or interface. Now you have the problem of how to provide the different objects to the client without the client having to know which class is being created. The answer is to create a factory class that has a method that takes a parameter which it uses to figure out which type of object to return.

Example Code

Here is an example in VB.NET of a factory pattern:
(Note: If large spaces appear between lines of code then widen your browser)

First the factory class. (Note: this could be implemented as a method in the client class rather than as a class.)

Public Class ProcessControllerFactory

Public Shared Function GetController( process As String) As ProcessController
Dim result As ProcessController

Select Case process.ToUpper()
Case "EXTRACT"
result = New ExtractController()
Case "IMPORT"
result = New ImportController()
Case "MATCHING"
result = New MatchingController()
Case "CONFIRMATION"
result = New ConfirmationController()
Case "DISTRIBUTION"
result = New DistributionController()
Case Else
result = Nothing
End Select

Return result
End Function

End Class



The client class gets the process controller it wants like this:

Public Sub Execute( action as String)
'declare the variable using the superclass as the type
Dim processCtrl As ProcessController
'now get the controller
processCtrl = ProcessControllerFactory.GetController(action)
'now call the method common to all classes derived from the superclass
processCtrl.runProcesses()
'other code here
End Sub


If the factory were implemented as a method then "Execute" would look like this:

Public Sub Execute( action as String)
Dim processCtrl As ProcessController
'calls its own method
processCtrl = GetController(action)
processCtrl.runProcesses()
'other code here
End Sub


The classes of the objects produced by the factory look like this:

Public Class ImportController
Inherits ProcessController

Public Overrides Sub runProcesses()
'code goes here
End Sub

End Class


And the super-class of all process controller classes looks like this:

Public MustInherit Class ProcessController

Public Overridable Sub runProcesses()
'default handling code goes here or it could be an abstract method
End Sub

End Class

Resources

Factory pattern example
The Factory Method Design Pattern by Gopalan Suresh Raj
Solve application issues with the factory pattern
Implementing the factory pattern using attributes and activation - C# Programming
The Simple Factory Pattern in C#

No comments: