G.Ashok,
| Double dispatch will not eliminate the switch statements.
Double Dispatch is how you solve it. Without switch statements!
Look closely at my initial example & the following sample. It just happened
that my example C was both Currency & AccountManager. The sample below
separates the two types.
Something like:
Module Module1
Sub Main()
Dim manager As New AccountManager
manager.handleTransfer(New BankAccount)
End Sub
End Module
Public Interface IBankAccount
Sub deposit(ByVal currency As Currency)
' routines to handle the double dispatch.
Sub doCurrencyDeposit(ByVal currency As Currency)
Sub doEuroCurrencyDeposit(ByVal currency As EuroCurrency)
End Interface
Public Class Currency
' routines to handle the double dispatch
Public Overridable Sub Deposit(ByVal account As IBankAccount)
account.doCurrencyDeposit(Me)
End Sub
End Class
Public Class EuroCurrency : Inherits Currency
' routines to handle the double dispatch
Public Overrides Sub Deposit(ByVal account As IBankAccount)
account.doEuroCurrencyDeposit(Me)
End Sub
End Class
Public Class BankAccount
Implements IBankAccount
Public Sub deposit(ByVal currency As Currency) Implements
IBankAccount.deposit
' ask the specific type of currency to call me back via the
specific routine...
currency.Deposit(Me)
End Sub
' only called by Currency objects
Public Sub doCurrencyDeposit(ByVal currency As Currency) Implements
IBankAccount.doCurrencyDeposit
' do the actual 'Currency' deposit here
End Sub
' only called by EuroCurrency objects
Public Sub doEuroCurrencyDeposit(ByVal currency As EuroCurrency)
Implements IBankAccount.doEuroCurrencyDeposit
' do the actual 'EuroCurrency' deposit here
End Sub
End Class
Public Class AccountManager
Public Sub handleTransfer(ByVal account As IBankAccount)
Dim currency As currency = New EuroCurrency '// getCurrency()
account.deposit(currency)
currency = new Currency
account.deposit(currency)
End Sub
End Class
NOTE: both deposit(Currency) & DoCurrencyDeposit(Currency) are needed in
IBankAccount to avoid an endless recursive loop, during the "callback" from
the double dispatch...
NOTE: Rather then have a class hierarchy for Currency I would consider
implementing a Money type as discussed by Kent Beck in "Test - Driven
Development - By Example" from Addison Wesley press.
http://www.nunit.org/samples.html
The "danger" of double dispatch is that DoCurrencyDeposit &
DoEuroCurrencyDeposit need to be public, which means they may be called
inappropriately, if all the types concerned are in the same assembly I would
consider making them Friend instead of Public...
| I hope this helps to undertsand the problem in detail :o)
I hope you understand my sample in detail! ;-)
Hope this helps
Jay
"G.Ashok" <gw******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP14.phx.gbl...
|
| Hi, Thank you all for the response in anyway,
|
| Here the story...
|
| Module Module1
| Sub Main()
|
| Dim am As New AccountManager
|
| am.handleTransfer(New BankAccount)
|
| End Sub
|
| End Module
|
| Public Interface IBankAccount
|
| Sub deposit(ByVal currency As Currency)
|
| 'Sub deposit(ByVal currency As EuroCurrency)
|
| End Interface
|
| Public Class Currency
|
| ' // These Currency classes could as easily be interfaces, with no impact
on
| the examples.
|
| ' // members
|
| '}
|
| End Class
|
| Public Class EuroCurrency : Inherits Currency
|
| '// members
|
| End Class
|
| Public Class BankAccount : Implements IBankAccount
|
| Public Sub deposit(ByVal currency As Currency) Implements
| IBankAccount.deposit
|
| '// handle standard deposit
|
| End Sub
|
| Public Sub deposit(ByVal currency As EuroCurrency) ' Implements
| IBankAccount.deposit
|
| '// handle Euro Currencies
|
| End Sub
|
| End Class
|
| Public Class AccountManager
|
| Public Sub handleTransfer(ByVal account As IBankAccount)
|
| Dim currency As Currency = New EuroCurrency '// getCurrency()
|
| '//
|
| '// Make a deposit. In standard Java and *.NET languages,
|
| '// this will always invoke BankAccount's first deposit method.
|
| '// With Multiple Polymorphism, this will invoke the second deposit
| method for
|
| '// EuroCurrency (and its derived classes), otherwise the first
| deposit method.
|
| account.deposit(currency)
|
| End Sub
|
| End Class
|
| Another workaround approach to the above problem is to create another
| interface method, defining the following:
|
| Public Interface IBankAccount
| Sub deposit(currency As Currency)
| Sub deposit(currency AS EuroCurrency)
| End Inteface
|
| But this would force all implementations to define a method for
| the second case, even if none were desired. This will further forces to
use
| several huge amount of switch (Select Case/If TypeOf ) statements
|
| With multiple polymorphism, if the currency returned by getCurrency is not
a
| EuroCurrency (inheritance is-a), the first deposit method defined in
| BankAcount will be called. If the currency is a EuroCurrency, the second
| (more specialized) deposit method will be called.
|
| Double dispatch will not eliminate the switch statements.
|
| I hope this helps to undertsand the problem in detail :o)
|
| Regards,
| ...Ashok
| ------------------------------------------------------------------------
| "It is beautiful for an engineer to shape and design the same way
| that an artist shapes and designs. But whether the whole process
| makes any sense, whether men become happier - that I can no
| longer decide." - Rudolph Diesel
|
|
| "G.Ashok" <gw******@hotmail.com> wrote in message
| news:eo**************@TK2MSFTNGP14.phx.gbl...
|
|| Hi,
||
|| Does anybody know how Multiple polymorphism can be done in VB.NET or DOT
|| doesn't support it? Is there any third party extensions available like
for
|| Java to do this?
||
|| Regards,
|| ...Ashok
|| ------------------------------------------------------------------------
|| "It is beautiful for an engineer to shape and design the same way
|| that an artist shapes and designs. But whether the whole process
|| makes any sense, whether men become happier - that I can no
|| longer decide." - Rudolph Diesel
||
||
|
|
|
|