These are the two object adapter classes. They use object
composition/class inheritance by holding a reference to the axle object (the
adaptee). They use their respective adaptation-functions to convert the
not-so-useful wheel-turn rate and wheel-turns into ground-speed and
miles-driven respectively. The functions which achieve these adaptations are
UpdateMileage and MeasureSpeed respectively.
Listing 3
Public Class Odometer
Private m_OdometerCable As Axle
Private m_MilesDriven As Double
Private PreviousSampleTime As Date
Public ReadOnly Property MilesDriven()
End Property
Public Function UpdateMileage(ByVal WheelTurnsPerMile As Single)
m_MilesDriven + = m_OdometerCable.WheelTurnRate / WheelTurnsPerMile / _
3600 * DateDiff(DateInterval.Second, PreviousSampleTime, Now())
PreviousSampleTime = Now()
End Function
End Class
Public Class Speedometer
Private Shared CarSpeedometer As Speedometer
Public m_SpeedometerCable As Axle
Private m_GroundSpeed As Integer
Private Sub New(ByVal AxleConnection)
m_SpeedometerCable = AxleConnection
End Sub
Public Function MeasureSpeed(ByVal WheelTurnsPerMile As Single) As Integer
m_GroundSpeed = m_SpeedometerCable.WheelTurnRate / WheelTurnsPerMile
Return m_GroundSpeed
End Function
End Class
MileMeter
This is the class adapter version. It inherits from the
axle class (the adaptee) while implementing Interface MileMeter. In order to
utilize it, we will instantiate it and call the Method which was implemented. Internally,
this method accesses the inherited method/property from the inherited class:
Me.WheelTurnRate. Notice that because WheelTurnRate is inherited, it is
internal to the class-adapter and so we do not make a reference looking like:
m_OdometerCable.WheelTurnRate.
Listing 4
Public Interface MileMeter
Function MeasureMilesDriven(ByVal WheelTurnsPerMile As Single) As Single
End Interface
Public Class CableReader ' Class adapter
Inherits SubAxle ' Inherit adaptee
Implements MileMeter ' Implement target interface
Dim PreviousSampleTime As DateTime
Public Sub New()
MyBase.New()
PreviousSampleTime = Now
End Sub
Public Function MeasureMilesDriven( _
ByVal WheelTurnsPerMile As Single) As Single Implements MileMeter.MeasureMilesDriven
Dim MileageChange As Single
MileageChange = Me.WheelTurnRate / WheelTurnsPerMile / _
3600 * DateDiff(DateInterval.Second, PreviousSampleTime, Now())
PreviousSampleTime = Now
Return MileageChange
End Function
End Class
Client
This is the class adapter version. It inherits from the
axle class (the adaptee) while implementing Interface MileMeter. In order to
utilize it, we will instantiate it and call the Method which was implemented. Internally,
this method accesses the inherited method/property from the inherited class:
Me.WheelTurnRate. Notice that because WheelTurnRate is inherited, it is
internal to the class-adapter and so we do not make a reference looking like:
m_OdometerCable.WheelTurnRate.
Listing 5
Private Sub Form1_Load(_
ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyAxle = Axle.GetAxle
WheelOnCar = New Wheel
WheelTurnsPerMile = 1600 / (WheelOnCar.OverallDiameter * Math.PI)
MyOdometer = Odometer.GetOdometer(MyAxle, LastMileageRecorded)
MySpeedometer = Speedometer.GetSpeedometer(MyAxle)
TripMeter = New CableReader
End Sub
Public Sub RenderSpeed(ByVal SpeedToDisplay As Integer)
Dim LocationOfSpeed As New Point
Dim AngleInRadians As Single
Const NeedleLength As Integer = 70
Const XOffset As Integer = 110 ' X coordinate of the point about which the needle rotates
Const YOffset As Integer = 100 ' Y coordinate of the point about which the needle rotates
Dim RotationPoint As New Point(XOffset, YOffset)
AngleInRadians = 3 * SpeedToDisplay * Math.PI / 180
LocationOfSpeed.X = -1 * (Math.Cos(AngleInRadians) * NeedleLength) + XOffset
LocationOfSpeed.Y = -1 * (Math.Sin(AngleInRadians) * NeedleLength) + YOffset
NeedleGraphics.Clear(Color.FromKnownColor(KnownColor.Control))
Dim NeedlePen As New Pen(Color.Red, 3)
NeedleGraphics.DrawLine(NeedlePen, RotationPoint, LocationOfSpeed)
End Sub
Private Sub vsbGasPedal_Scroll( … ) Handles vsbGasPedal.Scroll
MyAxle.WheelTurnRate = CInt(vsbGasPedal.Value) ' Object Adapter
If MyAxle.WheelTurnRate > 55000 Then
MyAxle.WheelTurnRate = 55000 ' Speed limited :-)
End If
TripMeter.WheelTurnRate = MyAxle.WheelTurnRate ' Class adapter
lblWheelTurnRate.Text = CInt(Me.MyAxle.WheelTurnRate / 3600)
DoDashboardUpdates()
End Sub
Private Sub Timer1_Tick( … ) Handles Timer1.Tick
DoDashboardUpdates()
End Sub
Public Sub DoDashboardUpdates()
RenderSpeed(MySpeedometer.MeasureSpeed(WheelTurnsPerMile))
MyOdometer.UpdateMileage(WheelTurnsPerMile)
lblMilesDriven.Text = Math.Floor(MyOdometer.MilesDriven)
TenthValueToDisplay = MyOdometer.MilesDriven * 10 Mod 10
If TenthValueToDisplay = 10 Then
TenthValueToDisplay = 0
End If
lblMilesDriven_Tenth.Text = TenthValueToDisplay
MilesInthisTrip + = TripMeter.MeasureMilesDriven(WheelTurnsPerMile)
lblMilesDrivenInTrip.Text = Math.Floor(MilesInthisTrip)
TenthValueToDisplay = MilesInthisTrip * 10 Mod 10
If TenthValueToDisplay = 10 Then
TenthValueToDisplay = 0
End If
lblTripMiles_Tenths.Text = (MilesInthisTrip * 10) Mod 10
End Sub
End Class