Abstract Document

Intent

Use dynamic properties and achieve flexibility of untyped languages while keeping type-safety.

Explanation

The Abstract Document pattern enables handling additional, non-static properties. This pattern uses concept of traits to enable type safety and separate properties of different classes into set of interfaces.


iluwatarStructuralExtensibilityAbout 2 min
Acyclic Visitor

Intent

Allow new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating the troublesome dependency cycles that are inherent to the GoF Visitor Pattern.

Explanation

Real world example

We have a hierarchy of modem classes. The modems in this hierarchy need to be visited by an external algorithm based on filtering criteria (is it Unix or DOS compatible modem).


iluwatarBehavioralExtensibilityAbout 2 min
Decorator

Also known as

Wrapper

Intent

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Explanation

Real-world example

There is an angry troll living in the nearby hills. Usually, it goes bare-handed but sometimes it has a weapon. To arm the troll it's not necessary to create a new troll but to decorate it dynamically with a suitable weapon.


iluwatarStructuralGang of FourExtensibilityAbout 3 min
Double Dispatch

Intent

Double Dispatch pattern is a way to create maintainable dynamic behavior based on receiver and parameter types.

Class diagram

Applicability

Use the Double Dispatch pattern when

  • the dynamic behavior is not defined only based on receiving object's type but also on the receiving method's parameter type.

iluwatarIdiomExtensibilityLess than 1 minute
Execute Around

Intent

Execute Around idiom frees the user from certain actions that should always be executed before and after the business method. A good example of this is resource allocation and deallocation leaving the user to specify only what to do with the resource.

Explanation

Real-world example


iluwatarIdiomExtensibilityAbout 1 min
Extension objects

Intent

Anticipate that an object’s interface needs to be extended in the future. Additional interfaces are defined by extension objects.

Class diagram

Applicability

Use the Extension Objects pattern when:

  • you need to support the addition of new or unforeseen interfaces to existing classes and you don't want to impact clients that don't need this new interface. Extension Objects lets you keep related operations together by defining them in a separate class
  • a class representing a key abstraction plays different roles for different clients. The number of roles the class can play should be open-ended. There is a need to preserve the key abstraction itself. For example, a customer object is still a customer object even if different subsystems view it differently.
  • a class should be extensible with new behavior without subclassing from it.

iluwatarBehavioralExtensibilityLess than 1 minute
Factory Kit

Also Known As

Abstract-Factory

Intent

Define a factory of immutable content with separated builder and factory interfaces.

Explanation

Real-world example

Imagine a magical weapon factory that can create any type of weapon wished for. When the factory is unboxed, the master recites the weapon types needed to prepare it. After that, any of those weapon types can be summoned in an instant.


iluwatarCreationalExtensibilityAbout 1 min
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.


iluwatarCreationalExtensibilityGang of FourAbout 2 min
Feature Toggle

Also known as

Feature Flag

Intent

Used to switch code execution paths based on properties or groupings. Allowing new features to be released, tested and rolled out. Allowing switching back to the older feature quickly if needed. It should be noted that this pattern, can easily introduce code complexity. There is also cause for concern that the old feature that the toggle is eventually going to phase out is never removed, causing redundant code smells and increased maintainability.


iluwatarBehavioralExtensibilityLess than 1 minute
Filterer

Name / classification

Filterer

Intent

The intent of this design pattern is to introduce a functional interface that will add a functionality for container-like objects to easily return filtered versions of themselves.

Explanation

Real world example


iluwatarFunctionalExtensibilityAbout 3 min
2