• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Double Dispatcher

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I want to know that is there any design pattern with the name "Double Dispatcher" design pattern.
Thanks
From shehzad
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is indeed. Drop the name "double dispatch" in Google and find some examples. Say a method gets a parameter declared as an abstract type. The actual parameter might be any subclass. Say the method has to take different action for each subclass. It might have a case statement testing for instanceof every known subclass. Big switch statement, can't handle new subclasses.
So in double dispatch, the parameter objects would know what to do differently. So our method just calls the parameter object passing itself, and the parameter object does the work.
Did you find that confusing? Me, too! Look around for some concrete examples and see if they make more sense.
The classic example is numbers. Say I have an Int class (not talking real Java now) with an "add" operator. a.add(b) gives a+b. The add method might check b to see if it's an int, float, double, etc. With double dispatch it could say b.addInt(self). Int, Float, Double, etc. would all have addInt() methods that do the proper casting or conversions or whatever is needed. No case statement!
[ April 30, 2003: Message edited by: Stan James ]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shehzi Khan:
Hello,
I want to know that is there any design pattern with the name "Double Dispatcher" design pattern.
Thanks
From shehzad



I understood double dispatching differently. This
is when a method "m" called on object "a" with object "b" as parameter and then in "m" a call to another method on "b" is made by passing "a" as parameter.
An example is Visitor pattern. You call Visitable.accept(Visitor) but inside accept method
the Visitable calls visit method passing itself as argument to the method.
Like Visitor.visit(Visitable)
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roshan,
Visitor is same as double dispatch. What you are talking about is implementation, while Stan is mentioning the intent behind that implementation
HTH
Bhushan
 
Shehzi Khan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
Thanks for all your help and consideration. I would now like to throw my question again but in some more detail.
Actually, I was studying Singleton pattern. For singleton, it is necessary to have one and only instance to be made and the method through which this would be done will be globally accessible. But there is a problem with singleton pattern that if, when that instance is going to be made for the first time, more than two people simultaneously tried to make the instance, two instances will be made which is a problem. We have the option of making our method or inner sub part of the method as synchronized but that would also be nonsense since it will only be usefull for the time when the instanec be made but then we will seriously downsize our application performance. Now from hereon, I come to know from someone that there is also a "double dispatcher" pattern which has a very close relationship with that singleton pattern. well, so now please can anyone explain what is double dispatch pattern.
thanks, take care
from shehzad
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, there is a double checked lock algorithm for singleton, once recommended in Sun samples, that is now discredited. Here's the classic double check

It looks tight enough, but given unpredictable compiler and runtime optimizations on multi-processor machines, it can fall apart. If you want to get into the internals and see why, try
Double Checked Locking or two Brian Goetz articles
(Just to be extra precise, all visitors may be double dispatched, but all double dispatches are not visitors. The Visitor Pattern is a neat way to build walking a case graph into the graph.)
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D'oh! Forgot the good part. If double check locking is out, what is in? Static creation:
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic