Abstract Factory Pattern in VB.NET

 The ABSTRACT FACTORY PATTERN comes under the classification of Creational Patterns. The creational patterns deals with the best way to create objects. The Abstract Factory provides an interface to create and return one of several families of related objects.

"Provide an interface for creating families of related or dependent objects without specifying their concrete classes" -- "Design Patterns" Gamma et al., Addison-Wesley, ISBN:0-201-63361-2".

Non-software Example

This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles. The stamping equipment is an Abstract Factory, which creates auto body parts. The same machinery is used to stamp right hand doors, left hand doors, right front fenders, left front fenders, hoods, etc. for different models of cars. Through the use of rollers to change the stamping dies, the concrete classes produced by the machinery can be changed within three minutes. [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54].

The abstract factory is a factory object that returns one of several factories. It can be used to return one of several related classes of objects, each of which can return several different objects on request.  

The abstract factory pattern can be interpreted and implemented in many ways. The following is a simple's interpretation and implementation of this pattern.  

AbstractFactPat-Vb.net.gif

In this case the interface Factory has two concrete implementations, ConcreteFactory1 and ConcreteFactory2. The getObject() inside these concrete classes returns Derived1 and Derived2 objects respectively. The client can decide which ConcreteFactory class has to be used during the run-times.  

The following is a more complicated interpretation and implementation of this pattern. Here those Factory class methods are used for returning objects of two different class hierarchies.  

AbstractFactPat2-Vb.net.gif

VB.NET Implementation

'Creational Pattern: Abstract Factory Pattern
'In the following snippet, Factory is an interface. 
'The concrete implementation of this interface 
'ConcreteFactory1 and ConcreteFactory2 
'implements the method getObject 
'so that it returns Derived1 and Derived2 objects respectively. 
'The Base is an interface and Derived1 and 
'Derived2 are the concrete 
'implementations of the base class. 
'The client (MyClient class) always uses the Factory implementations 
'to create an instance of the Base classes. 
'Actually the derived classes of Factory interface decided 
'which object (either Derived1 or Derived2) has to be created.

Imports System
Interface Factory
Function GetObject() As Base
End Interface 'Factory
'This class is responsible for creating objects of the class Derived1.
Class ConcreteFactory1
Inherits Factory
Public Function GetObject() As Base
Return New Derived1
End Function
'GetObject
End Class 'ConcreteFactory1
'This class is responsible for creating objects of the class Derived2.
Class ConcreteFactory2
Inherits Factory
Public Function GetObject() As Base
Return New Derived2
End Function 'GetObject
End Class 'ConcreteFactory2_
Interface Base
Sub DoIt()
End Interface 'Base
Class Derived1
Inherits Base
Public Sub DoIt()
Console.WriteLine("Derived 1 method")
End Sub 'DoIt
End Class 'Derived1
Class Derived2
Inherits Base
Public Sub DoIt()
Console.WriteLine("Derived 2 method")
End Sub 'DoIt
End Class 'Derived2
'
'Client class needn't know about instance creation. The creation of Product 
'is deferred to he ConcreteFactory1.
'
Class MyClient
Public Shared 
Sub Main()
Dim factory = New ConcreteFactory2
'Decides which object must create.
Dim obj As Base = factory.GetObject()
obj.DoIt()
End Sub 'Main
End Class 'MyClient


Similar Articles