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]
The classifications and typical uses are
PatternCategoryWhen to use it
Command patternBehavioral design patternRepresent 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 patternBehavioral design patternSelect among interchangeable algorithms through a common interface, such as different routing or scoring policies.
Adapter patternStructural design patternWrap an existing component so clients can use it through a different required interface without changing that component.
State patternBehavioral design patternDelegate behavior to an object representing the current state, with transitions selecting another state object instead of scattering conditionals across operations.
Visitor patternBehavioral design patternAdd operations across a relatively stable element-class hierarchy, commonly through double dispatch. Adding a new element type then usually requires updating the visitors.