My personal notes on the book Design Patterns Explained Simply.
Creational patterns
- Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
- Abstract factory: provides an interface for creating families of related or dependent objects without specifying their concrete class.
- Builder
- Separates the construction of a complex object from its representation, so that the same construction process can create different representations.
- Parses a complex representation, creates a one of several targets.
- Common input + many possible representations = good candidate for builder pattern.
- Factory method
- Defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to its subclasses.
- If you have an inheritance hierarchy that exercises polymorphism, consider adding a polymorphic creation capability by definining a
static
factory method in the base class.
- Object pool
- Object pooling can offer a significante performance boost; it’s most effective in situations where the cost of initializing an object is high.
- They are sed to manage object calling.
Structural patterns
- These design patterns improve the design by identifying a simple way to realize relationships between entities.
- Decorator
- It dynamically attaches additional responsibilities to an object. Decorators provide a flexible alternative to subclassing for extending functionality.
- This solves the problem where you want to add behaviour or state to individual objects at runtime.
- Facade
- Provides an unified interface to a set of interfaces in a subsystem.
- Wraps a complicated subsystem with a simple interface.
- Problem: a segment of the client community needs a simplified inteface to the overall functionaly of a complex subsystem.
- It should not be a “god” object.
- Proxy
- Provides a surrogate or placeholder for another object to control access to it.
- Useful when you need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.
- Adapter
- It converts the interface of a class into another interface clients expect. Adapter lets classes work together that otherwise couldn’t because of incompatible interfaces.
- Adapter makes things work after they’re designed; Bridge makes them work before they are.
- Bridge
- It decouples an abstraction from its implementation so that the two can vary independently.
- It decomposes the component’s interface and implementation into orthogonal class hierarchies.
- Its core feature is using interfaces.
Behavioral patterns
- These patterns identify common communication patterns between objects and realize these patterns.
- Chain of responsibility
- It avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
Client --- request ---> Chain { Processing Element -> Processing Element -> Processing Element}
- Command
- It encapsulates a request as an object, thereby letting you parametrize clients with different requests, and support undoable operations.
- Problem: needs to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.
- Iterator
- Provides a way to access the elements of a collection without exposing its underlying representation.
- Mediator
- It defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
- Memento
- Without violating encapsulation, capture and externalize an object’s internal state, so that the object can be returned to this state later.
- Problem: need to restore an object back to its previous state (e.g. “undo” or “rollback” operations).
- Null object
- The intent of a Null Object is to encapsulate the absence of an object by providing an alternative object that offer suitable default “do nothing” behaviour.
- Observer
- It defines a one-to-many dependency between objects so that when an object changes state, all its dependents are notifies and updated automatically.
- State
- Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
- It’s like an object-oriented state machine.
- The
State
is encapsulated by aContext
class.
- Strategy
- It defines a family of algorithms, encapsulates them, and makes them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
- “Program to an interface, not an implementation.”
- Visitor
- It represents an operation to be performed on the objects of an object structure. Visitor lets you define a new operaton without changing the classes of the elements on which it operates.