Understanding Adapter Pattern Using .NET
page 7 of 15
by David Simmonds
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 53687/ 127

Odometer and Speedometer

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 SingleAs 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 SingleAs 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 SingleAs 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

View Entire Article

User Comments

Title: its great explaination in detail about Adapter Pattern   
Name: Elan Emerging Technologies Pvt. Ltd
Date: 2010-01-18 7:05:35 AM
Comment:
its great explaination in detail about Adapter Pattern


Thanks,
Elan Emerging Technologies (EETPL)
http://www.elantechnologies.com
Title: Thanks   
Name: Rakesh
Date: 2008-12-08 2:59:28 PM
Comment:
Looking forward to read this article
Title: Very Understandable   
Name: Srilakshmi15
Date: 2008-05-11 7:19:36 AM
Comment:
Hi Simmonds
It's really good to understand without getting any confusion.
Thanks a lot.
Title: Not able to Download the Codes   
Name: Ritesh
Date: 2007-08-07 4:19:25 AM
Comment:
I am not able to download the codes of the above sample
Title: Great! Resouces.   
Name: .Net Developer
Date: 2007-05-14 1:56:04 AM
Comment:
It's Great Resouces for abt adapter pattern. I hope you will writing usefull .net articles in future.

Jim
http://www.tatvasoft.com
Title: its great explaination in detail abt adapter pattern   
Name: vamshi
Date: 2006-11-14 5:27:20 AM
Comment:
its great explaination in detail abt adapter pattern with a clear explanation -not found any where else. hope i will get other pattern too
Title: Adapter Explanation with real Time   
Name: Ranjith M
Date: 2006-11-03 4:03:56 AM
Comment:
Its realy cool.I went through lot of links i dont understand the pattern.The real time explanation is realy good one.Thanks a Lot.:)

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 11:13:11 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search