I've been learning how to do Object Composition in Javascript using Concatenative Inheritance and wondered how I could accomplish something similar in VBA (which doesn't have inheritance).
I've created a simple example to demonstrate what I'd like to accomplish.
Testing Module
This is just an example use of the Fighter
class. The Fight
method is actually calling the Fight
method in the CanFight
class. It debugs a message and reduces stamina by 1.
Private Sub StartGame()
Dim Slasher As Fighter
Set Slasher = New Fighter
Slasher.Name = "Slasher"
Slasher.Fight '-> Slasher slashes at the foe!
Debug.Print Slasher.Stamina '-> 99
End Sub
Fighter Class
This class has two public properties Name
and Stamina
.
This class also contains FightAbility
which is an instance of the CanFight
class. This is my attempt at trying to accomplish composition.
Option Explicit
Private FightAbility As CanFight
Private pName As String
Private pStamina As Long
Private Sub Class_Initialize()
pStamina = 100
Set FightAbility = New CanFight
End Sub
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Name(ByVal Value As String)
pName = Value
End Property
Public Property Get Stamina() As String
Stamina = pStamina
End Property
Public Property Let Stamina(ByVal Value As String)
pStamina = Value
End Property
'This is the function that uses the ability to fight.
'It passes a reference to itself to the `CanFight` class
'giving it access to its public properties.
'This is my attempt at composition.
Public Sub Fight()
FightAbility.Fight Me
End Sub
CanFight Class
This is the class that can be reused for other characters. An Example is a Paladin
class might need to also have the ability to fight.
The obvious issue with how this is laid out is that state
is an Object
. The user won't know it needs to have Stamina
and a Name
property unless they look at the code.
Option Explicit
Public Sub Fight(ByRef State As Object)
Debug.Print State.Name & " slashes at the foe!"
State.Stamina = State.Stamina - 1
End Sub
My example feels broken since there is no structure in place as far as what properties are needed in order to use it. At the same time, I want to make sure my game characters can be flexible in having their own distinct properties as well (for example a Paladin
class might have a Mana
property).
My question is how do I achieve structured Composition like this in VBA?
Aucun commentaire:
Enregistrer un commentaire