Double dispatch Created 2026-10-31 Updated 2026-11-05
Double dispatch selects behavior using the runtime types of two participating software objects, often by arranging two ordinary dynamic dispatch steps. The visitor pattern supplies a useful example.
Suppose
Shape has an overridable accept(Renderer) operation, and Renderer has overridable overloads visit(Circle) and visit(Rectangle). A Circle implementation of accept calls renderer.visit(this). Calling shape.accept(renderer) first selects Circle.accept using the shape's runtime type. Inside that method, this has the static type Circle because the method is defined in the Circle software class. Method overloading therefore chooses the visit(Circle) signature. The renderer's runtime type then selects the implementation of that signature.The result depends on both the concrete shape and concrete renderer. Ordinary method overloading alone would not inspect an argument's runtime type to choose the shape overload. The static type established in the first dispatched method is the bridge to the second dispatch.[ref]
Scala 2.11 expressions specification. Overloading Resolution, static argument typing; example uses ordinary overridable instance methods
The classifications and typical uses are
| Pattern | Category | When to use it |
|---|---|---|
| Command pattern | Behavioral design pattern | Represent a request as an object, separating its invocation from the receiver. Commands can be queued or logged; undo additionally needs enough information to reverse the particular request. |
| Strategy pattern | Behavioral design pattern | Select among interchangeable algorithms through a common interface, such as different routing or scoring policies. |
| Adapter pattern | Structural design pattern | Wrap an existing component so clients can use it through a different required interface without changing that component. |
| State pattern | Behavioral design pattern | Delegate behavior to an object representing the current state, with transitions selecting another state object instead of scattering conditionals across operations. |
| Visitor pattern | Behavioral design pattern | Add operations across a relatively stable element-class hierarchy, commonly through double dispatch. Adding a new element type then usually requires updating the visitors. |