Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
545 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Hi,
I recently came across the code below.
If you have the time, could you please make a video or blog explaining what is going on here?
I suspect it's an advanced topic, probably class factories or something similar.
Thanks
''''''''''''''''''''''Standard module:
Option Explicit
Private Enum TransportationMode
ByBus = 1
ByTrain
ByPlane
End Enum
Public Sub Main()
Dim myPlanner As TravelPlanner
Set myPlanner = New TravelPlanner
myPlanner.SetTravelStrategy Travelby(ByBus)
myPlanner.Drive 1000
myPlanner.SetTravelStrategy Travelby(ByTrain)
myPlanner.Drive 1000
myPlanner.SetTravelStrategy Travelby(ByPlane)
myPlanner.Drive 900
Set myPlanner = Nothing
End Sub
Private Function Travelby(ByVal Mode As TransportationMode) As IStrategy
Set Travelby = GetListOfObjects.Item(VBA.CStr(Mode))
End Function
Private Function GetListOfObjects() As Collection
Static Coll As Collection
If Coll Is Nothing Then
Set Coll = New Collection
Coll.Add New Bus, VBA.CStr(TransportationMode.ByBus)
Coll.Add New Train, VBA.CStr(TransportationMode.ByTrain)
Coll.Add New Plane, VBA.CStr(TransportationMode.ByPlane)
End If
Set GetListOfObjects = Coll
End Function
''''''''''''''''''''''Class Module Bus:
Option Explicit
Implements IStrategy
Private Const MYMODE As String = "Bus"
Private Property Get IStrategy_costPerKilometer() As Single
IStrategy_costPerKilometer = 20
End Property
Private Property Get GetGeneralMethods() As Misc
Set GetGeneralMethods = New Misc
End Property
Private Sub IStrategy_drive(ByVal kilometers As Long)
Dim totalCost As Double
totalCost = GetGeneralMethods.CalculateCostOfTravel(kilometers, IStrategy_costPerKilometer)
GetGeneralMethods.ShowResultOnImmediateWindow MYMODE, kilometers, totalCost
End Sub
''''''''''''''''''''''Class Module IStrategy:
Option Explicit
Public Property Get costPerKilometer() As Single
End Property
Public Sub Drive(ByVal kilometers As Long)
End Sub
''''''''''''''''''''''Class Module Misc:
Option Explicit
Public Sub ShowResultOnImmediateWindow(ByVal TravelWith As String, ByVal kilometers As Long, ByVal totalCost As Double)
Debug.Print "Total cost of travel with " & TravelWith & " for " & kilometers & " KM is : " & VBA.Format(totalCost, "Standard")
End Sub
Public Function CalculateCostOfTravel(ByVal kilometers As Single, ByVal costOfPerKilometers As Long) As Double
CalculateCostOfTravel = kilometers * costOfPerKilometers
End Function
''''''''''''''''''''''Class Module Plane:
Option Explicit
Implements IStrategy
Private Const MYMODE As String = "Air Plane"
Private Property Get IStrategy_costPerKilometer() As Single
IStrategy_costPerKilometer = 50
End Property
Private Property Get GetGeneralMethods() As Misc
Set GetGeneralMethods = New Misc
End Property
Private Sub IStrategy_drive(ByVal kilometers As Long)
Dim totalCost As Double
totalCost = GetGeneralMethods.CalculateCostOfTravel(kilometers, IIf(kilometers > 1000, 25, IStrategy_costPerKilometer))
GetGeneralMethods.ShowResultOnImmediateWindow MYMODE, kilometers, totalCost
End Sub
''''''''''''''''''''''Class Module Train:
Option Explicit
Implements IStrategy
Private Const MYMODE As String = "Train"
Private Property Get IStrategy_costPerKilometer() As Single
IStrategy_costPerKilometer = 10
End Property
Private Property Get GetGeneralMethods() As Misc
Set GetGeneralMethods = New Misc
End Property
Private Sub IStrategy_drive(ByVal kilometers As Long)
Dim totalCost As Double
totalCost = GetGeneralMethods.CalculateCostOfTravel(kilometers, IStrategy_costPerKilometer)
GetGeneralMethods.ShowResultOnImmediateWindow MYMODE, kilometers, totalCost
End Sub
''''''''''''''''''''''Class Module TravelPlanner:
Option Explicit
Public TravelStrategy As IStrategy
Public Sub SetTravelStrategy(ByVal myStrategy As IStrategy)
Set TravelStrategy = myStrategy
End Sub
Public Sub Drive(ByVal TravelDistance)
TravelStrategy.Drive TravelDistance
End Sub
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.