Our training courses

Other training resources

Our training venues

Why we are different

Details for captain

captain has participated in the following threads:

Added by captain on 29 Jan 2022 at 14:06

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

 

Head office

Kingsmoor House

Railway Street

GLOSSOP

SK13 2AA

London

Landmark Offices

99 Bishopsgate

LONDON

EC2M 3XD

Manchester

Holiday Inn

25 Aytoun Street

MANCHESTER

M1 3AE

© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.

End of small page here
Please be aware that our website uses cookies!
I'm OK with this Tell me more ...