Skip to main content
Double Dispatch

Also known as

  • Multi-methods

Intent

The Double Dispatch pattern is used to achieve dynamic polymorphism based on the types of two objects involved in a method call. It allows method behavior to be different based on the combination of the runtime types of both the object on which the method is called and the object being passed as a parameter.


iluwatarAbout 3 minBehavioralPolymorphism
Extension Objects

Also known as

  • Interface Extensions

Intent

The Extension Objects pattern allows for the flexible extension of an object's behavior without modifying its structure, by attaching additional objects that can dynamically add new functionality.

Explanation


iluwatarAbout 3 minStructuralEncapsulationExtensibilityObject compositionPolymorphism
Factory Method

Also known as

Virtual Constructor

Intent

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Explanation

Real-world example

Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.


iluwatarAbout 2 minCreationalEncapsulationGang of FourInstantiationObject compositionPolymorphism