I would like to know if it is possible to apply to the Factory Method project pattern within the limitations of VBA. If it is possible, would you have an example?
In the default module create:
modEnumPizza
Public Enum enumTypePizza
Pepperoni
Cheese
End Enum
modMain
Sub Main()
Dim objPizza As IPizza
Dim objFactory As Factory
Set objFactory = New Factory
Set objPizza = objFactory.CreatingPizza(Cheese, "Cheese Pizza", "mozzarella, oregano, olive, simple sauce", 25.99)
Debug.Print "Name: " & objPizza.getName
Debug.Print "Ingredients: " & objPizza.getIngredients
Debug.Print "Price: " & objPizza.getPrice
Set objPizza = objFactory.CreatingPizza(Pepperoni, "Pepperoni Pizza", "Calabresa, onion, olive, oregano", 30.99)
Debug.Print "Name: " & objPizza.getName
Debug.Print "Ingredients: " & objPizza.getIngredients
Debug.Print "Price: " & objPizza.getPrice
End Sub
In the class module create:
IPizza
Sub create(ByVal name As String, ByVal ingredients As String, ByVal price As Double): End Sub
Property Get getName() As String: End Property
Property Get getIngredients() As String: End Property
Property Get getPrice() As Long: End Property
clsCheese
Implements IPizza
Implements ICreate
Private Type TType
name As String
ingredients As String
price As Double
End Type
Private this As TType
Private Sub IPizza_create(ByVal name As String, ByVal ingredients As String, ByVal price As Double)
With this
.name = name
.ingredients = ingredients
.price = price
End With
End Sub
Private Property Get IPizza_getIngredients() As String
IPizza_getIngredients = this.ingredients
End Property
Private Property Get IPizza_getName() As String
IPizza_getName = this.name
End Property
Private Property Get IPizza_getPrice() As Long
IPizza_getPrice = this.price
End Property
clsPepperoni
Implements IPizza
Private Type TType
name As String
ingredients As String
price As Double
End Type
Private this As TType
Private Sub IPizza_create(ByVal name As String, ByVal ingredients As String, ByVal price As Double)
With this
.name = name
.ingredients = ingredients
.price = price
End With
End Sub
Private Property Get IPizza_getIngredients() As String
IPizza_getIngredients = this.ingredients
End Property
Private Property Get IPizza_getName() As String
IPizza_getName = this.name
End Property
Private Property Get IPizza_getPrice() As Long
IPizza_getPrice = this.price
End Property
Factory
Function CreatingPizza(ByVal enumPizza As enumTypePizza, ByVal name As String, ByVal ingredients As String, ByVal price As Double) As IPizza
Dim objPizza As IPizza
If enumPizza = Cheese Then
Set objPizza = New clsCheese
ElseIf enumPizza = Pepperoni Then
Set objPizza = New clsPepperoni
End If
objPizza.create name, ingredients, price
Set CreatingPizza = objPizza
End Function
For details on the Factory default, you can refer to the "Use Head - Design Patterns" book.
Run Main from the modMain module and see the Verification window (CTRL + G).
Note that in the 'Factory' class we have the 'CreatingPizza' function, where we select the type of pizza being created, by means of Enum type (enumPizza). If we have dozens of different pizzas to create we will have dozens of IF's. If we have to get flavors from the module modEnumPizza, we will have to change 'CreatingPizza'.
In addition, the 'create' method is visible in IntelliSense. If this and fact that we have many IFs does not please you, one solution, which is what I use is this:
Create an interface responsible for the 'create' method:
ICreateCheese
Function create(ByVal name As String, ByVal ingredients As String, ByVal price As Double) As IPizza: End Function
ICreatePepperoni
Function create(ByVal name As String, ByVal ingredients As String, ByVal price As Double) As IPizza: End Function
Now remove the 'crete' method from IPizza:
IPizza
Property Get getName() As String: End Property
Property Get getIngredients() As String: End Property
Property Get getPrice() As Long: End Property
Now clsChesse and clsPepperoni respectively implement ICreateChesse and ICreatePepperoni.
clsCheese
Implements IPizza
Implements ICreateChesse
Private Type TType
name As String
ingredients As String
price As Double
End Type
Private this As TType
Private Function ICreateChesse_create(ByVal name As String, ByVal ingredients As String, ByVal price As Double) As IPizza
With this
.name = name
.ingredients = ingredients
.price = price
End With
Set ICreateChesse_create = Me
End Function
Private Property Get IPizza_getIngredients() As String
IPizza_getIngredients = this.ingredients
End Property
Private Property Get IPizza_getName() As String
IPizza_getName = this.name
End Property
Private Property Get IPizza_getPrice() As Long
IPizza_getPrice = this.price
End Property
clsPepperoni
Implements IPizza
Implements ICreatePepperoni
Private Type TType
name As String
ingredients As String
price As Double
End Type
Private this As TType
Private Function ICreatePepperoni_create(ByVal name As String, ByVal ingredients As String, ByVal price As Double) As IPizza
With this
.name = name
.ingredients = ingredients
.price = price
End With
Set ICreatePepperoni_create = Me
End Function
Private Property Get IPizza_getIngredients() As String
IPizza_getIngredients = this.ingredients
End Property
Private Property Get IPizza_getName() As String
IPizza_getName = this.name
End Property
Private Property Get IPizza_getPrice() As Long
IPizza_getPrice = this.price
End Property
Factory
Implements ICreateChesse
Implements ICreatePepperoni
Private Type TType
'Could be a Dictionary References => Microsoft Scripting Runtime
objCollection As Collection
objPizza As IPizza
End Type
Private this As TType
Private Sub Class_Initialize()
Dim objInterfaceChesse As ICreateChesse
Dim objInterfacePepperoni As ICreatePepperoni
Set objInterfaceChesse = Me
Set objInterfacePepperoni = Me
Set this.objCollection = New Collection
'We now have a collection of instance variables pointing to ICreateChesse_create and
'ICreatePepperoni_create
With this.objCollection
.Add objInterfaceChesse, CStr(enumTypePizza.Cheese)
.Add objInterfacePepperoni, CStr(enumTypePizza.Pepperoni)
End With
End Sub
Function CreatingPizza(ByVal enumPizza As enumTypePizza, ByVal name As String, ByVal ingredients As String, ByVal price As Double) As IPizza
Dim objInterface As Object
'we return the corresponding instance variable
Set objInterface = this.objCollection(CStr(enumPizza))
'method call by late binding
Set CreatingPizza = objInterface.create(name, ingredients, price)
End Function
Private Function ICreateChesse_create(ByVal name As String, ByVal ingredients As String, ByVal price As Double) As IPizza
Dim objPizza As clsCheese
Dim objInterfaceChesse As ICreateChesse
'We create an instance of clsPizza
Set objPizza = New clsCheese
'We point to the interface address in clsChesse
Set objInterfaceChesse = objPizza
'I call the 'create' method of the instance of clsPizza, which returns the interface I`Pizza
Set ICreateChesse_create = objInterfaceChesse.create(name, ingredients, price)
Set objPizza = Nothing
Set objInterfaceChesse = Nothing
End Function
Private Function ICreatePepperoni_create(ByVal name As String, ByVal ingredients As String, ByVal price As Double) As IPizza
Dim objPizza As clsPepperoni
Dim objInterfacePepperoni As ICreatePepperoni
Set objPizza = New clsPepperoni
Set objInterfacePepperoni = objPizza
Set ICreatePepperoni_create = objInterfacePepperoni.create(name, ingredients, price)
Set objPizza = Nothing
Set objInterfacePepperoni = Nothing
End Function
ModMain does not change.
And the project looks like this:
Another alternative that avoids the use of a Factory class data structure would be this: Create an interface called ISelectCreator, where we can return by intelliSense which interface to use: ICreateChesse or ICreatePepperoni.
With this we can exclude the module modEnumPizza. In addition, the modMain module will also be modified, as the Factory class will change.
Here's the new refactoring in the Factory class and the creation of the ISelectCreator interface:
ISelectCreator
Property Get getCreateChesse() As ICreateChesse: End Property
Property Get getCreatePepperoni() As ICreatePepperoni: End Property
Factory
Implements ISelectCreator
Function CreatingPizza() As ISelectCreator
'with this intelliSense will show: getCreateChesse and getCreatePepperoni
Set CreatingPizza = Me
End Function
Private Property Get ISelectCreator_getCreateChesse() As ICreateChesse
Dim objPizza As clsCheese
Set objPizza = New clsCheese
Set ISelectCreator_getCreateChesse = objPizza
End Property
Private Property Get ISelectCreator_getCreatePepperoni() As ICreatePepperoni
Dim objPizza As clsPepperoni
Set objPizza = New clsPepperoni
Set ISelectCreator_getCreatePepperoni = objPizza
End Property
The Factory class is much simpler.
modMain: Main
Sub Main()
Dim objPizza As IPizza
Dim objFactory As Factory
Set objFactory = New Factory
Set objPizza = objFactory.CreatingPizza.getCreateChesse.create("Cheese Pizza", "mozzarella, oregano, olive, simple sauce", 25.99)
Debug.Print "Name: " & objPizza.getName
Debug.Print "Ingredients: " & objPizza.getIngredients
Debug.Print "Price: " & objPizza.getPrice
Set objPizza = objFactory.CreatingPizza.getCreatePepperoni.create("Pepperoni Pizza", "Calabresa, onion, olive, oregano", 30.99)
Debug.Print "Name: " & objPizza.getName
Debug.Print "Ingredients: " & objPizza.getIngredients
Debug.Print "Price: " & objPizza.getPrice
End Sub
Output:
Actually this is the way I currently use it. So that's it, Factory Method is possible yes.