3

Please see the link here: https://lostechies.com/derekgreer/2010/04/19/double-dispatch-is-a-code-smell/, which describes Double Dispatch as a code smell. Now see this link: https://lostechies.com/jimmybogard/2010/03/30/strengthening-your-domain-the-double-dispatch-pattern/, which talks about double dispatch strenghtening the domain. The same author of the second link (who believes in Double Despatch) wrote the code here: https://github.com/jbogard/presentations/blob/master/WickedDomainModels/After/Model/Member.cs and specifically this (which I believe is the Double Despatch pattern):

public Offer AssignOffer(OfferType offerType, IOfferValueCalculator valueCalculator) 
{ 
    DateTime dateExpiring = offerType.CalculateExpirationDate(); 
    int value = valueCalculator.CalculateValue(this, offerType); 
    var offer = new Offer(this, offerType, dateExpiring, value); 
    _assignedOffers.Add(offer); 
    NumberOfActiveOffers++; 
    return offer; 
} 

From what I can understand; the first author is saying that double despatch is an anti pattern because it violates SOLID principles. However, I believe the SOLID principles are violated in his code because of the way he has designed the classes; not specifically because of Double despatch. I also notice that the two articles were written within one month of each other in 2010.

Is Double Dispatch an anti pattern?

6

No, double dispatch is not an antipattern, it is a perfectly valid technique.

What the article describes is the expression problem. You want to be able to add new shapes and new drawing surfaces without violating the open/closed principle. Double dispatch is a possible solution to this problem. But it is possible to use it in such a way that it violates the open/closed principle, e.g. if you require each shape to have a draw method for each surface, then you would have to modify all shapes when adding a new drawing surface. If for some reason you want to avoid that, you could solve it by having some rendering adapters between the shapes and the surfaces, and use double dispatch to invoke the correct adapter.

In any case, even if some technique is in violation of the open/closed principle or SOLID in general, that does not make it an antipattern. SOLID is not an absolute good, it is just a set of guidelines (grouped under a catchy name) which are context-dependent and have limited applicability.

  • Thanks. Would you describe the code in my question as using Double despatch? I guess the way I have used it is not an anti pattern? +1 for reference to the expression problem. – w0051977 Jan 3 '18 at 11:03
  • 1
    @w0051977: The quoted code does not use DD. DD is when a function is selected based on the run-time types of two parameters. This code just contains two instances of single-dispatch. – JacquesB Jan 3 '18 at 11:24
  • how would you describe the code in my question using a pattern? Is there a GOF pattern? I am trying to document the code. – w0051977 Jan 3 '18 at 15:22
  • Can you answer my last comment? Then I should be in a position to mark the question as answered. – w0051977 Jan 3 '18 at 18:51
  • @w0051977: No I cannot answer that. Only you can document the intention of your code. – JacquesB Jan 3 '18 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.