Creational
Abstract factory
Factory method
-
Java
Traditional approach is based on
abstract class
.The better modern approach is based on
interface
anddefault method
.
Singleton
Prototype
- Specifies the kind of objects to create using a prototypical instance.
- The client replaces all references to the
new
operator with calls to the factory method.
Builder
-
Java
java.util.stream.Stream
Structural
Adapter
-
Intent
Converts one interface to another so that it matches what the client is expecting
Facade
-
Intent
Provides a simplified interface
Flyweight
- As flyweight objects are shared, they are usually immutable.
- Examples
- String interning (opens in a new tab)
- JDK:
java.lang.String
- JDK:
- Hash consing (opens in a new tab)
- Function as a caching mechanism:
- JDK:
java.util.WeakHashMap
- ES6:
WeakMap
- String interning (opens in a new tab)
Decorator
-
Intent
Dynamically adds responsibility to the interface by wrapping the original code
-
Notes
- Adding new behaviors to an individual object dynamically
- A flexible alternative to subclassing for extending functionality
- When using subclassing, different subclasses extend a class in different ways. But an extension is bound to the class at compile-time and can't be changed at runtime.
- The desired behaviors can be composed by adding different decorator objects to the base class object.
- Both the target object and the decorator implement the same interface.
-
Java
- Use
Lambda
andjava.util.function.Function
to simplify creation of decorator objects
- Use
-
Resources
Composite
-
Compose objects into tree structures to represent part-whole hierarchies.
-
Lets clients treat individual objects and compositions of objects uniformly.
-
Resources
Behavioral
Chain-of-responsibility
- Make requests pass a single processing pipeline that contains many possible handlers.
- Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.
- Examples
Template Method
- Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure
- Base class declares algorithm 'placeholders', and derived classes implement the placeholders
Observer
- Subject holds a list of Observers, and notifies them by calling their
update
method when target action is triggered - Reactive Programming is a modern Observer pattern implementation
Command
- Create objects encapsulating actions and parameters
- FP: Could use lambda to simplify creation of action objects
Iterator
-
Access the elements of an aggregate object in a sequential way
-
Abstract out the traversal of an aggregate object
-
Decouple data structure and algorithms
-
Java
java.lang.Iterable
State
-
Can be used to model a state machine
-
Allow an object to alter its behavior when its internal state changes
-
Java
- Use
sealed class
to limit the number of states and make the state machine more robust
- Use
Strategy
-
Notes
- Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
-
Java
- Use
Lambda
to simplify creation of strategy objects java.util.Comparator
- Use
Visitor
-
Can be better implemented with
pattern matching
andsealed class
-
Java
-
java.nio.file.FileVisitor
To implement a different behavior for each type of file, you can create a new
FileVisitor
implementation.
-
-
Java Magazine - The Visitor Design Pattern in Depth (opens in a new tab)