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

Participants – Sample Code

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

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-04-25 7:25:57 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search