Target–Miles driven
The interface which is useful to the client (driver). In
this case it is a non-existent class whose main member variable is
miles-driven.
Adaptee – Axle
Exposes an interface which is not usable by the client
(m_WheelTurnRate) and so needs adaptation.
Adapter – Odometer/Speedometer
Converts the interface exposed by the axle into the
interface expected by the driver. In the case of the Speedometer, it converts
wheel-turn rate to ground speed in mph. The odometer in the sample code is an
object adapter which accesses the state within the Axle object which it
references. The odometer is a class adapter which inherits from the Axle class
and converts the number of wheel turns into distance traveled.
Client – DoDashboardUpdates
Utilizes the converted method or state which the adapter
makes available.
Axle (object adapter example)
As discussed earlier, the axle is doing its thing, spinning
away and turning the wheels. So what we get from the axle is just the
wheel-turn rate and number of wheel-turns. It has nothing to do with the
pattern in the sense that it has no knowledge of the other participants in the
pattern, even though the other participants know about it. This is typical of
adaptees.
Listing 1
Public Class Axle ' Turns the wheels
Private Shared CarAxle As Axle
Private m_WheelTurnRate As Integer
Public Shared Function GetAxle() As Axle
If CarAxle Is Nothing Then
CarAxle = New Axle
End If
Return CarAxle
End Function
Public Property WheelTurnRate() As Integer
Get
Return m_WheelTurnRate
End Get
Set(ByVal Value As Integer)
m_WheelTurnRate = Value
End Set
End Property
End Class
Sub-Axle (class adapter example)
This is just another incarnation of the axle which we
include for convenience in order to demonstrate the class-adapter setup which
is utilized by the odometer.
Listing 2
Public Class SubAxle 'Turns the wheels
Friend Sub New()
End Sub
Private m_WheelTurnRate As Integer
Public Property WheelTurnRate() As Integer
Get
Return m_WheelTurnRate
End Get
Set(ByVal Value As Integer)
m_WheelTurnRate = Value
End Set
End Property
End Class