When to use Flashcards
You want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime
Strategy
You have a lot of similar classes that only differ in the way they execute some behavior.
Strategy
You want isolate the business logic of a class from the implementation details of algorithms that may not be as important in the context of that logic.
Strategy
Your class has a massive conditional statement that switches between different variants of the same algorithm.
Strategy
Changes to the state of one object may require changing other objects, and the actual set of objects is unknown beforehand or changes dynamically.
Observer
Some objects in your app must observe others, but only for a limited time or in specific cases.
Observer
You need to be able to assign extra behaviors to objects at runtime without breaking the code that uses these objects.
Decorator
It’s awkward or not possible to extend an object’s behavior using inheritance.
Decorator
You don’t know beforehand the exact types and dependencies of the objects your code should work with.
Factory Method
You want to provide users of your library or framework with a way to extend its internal components
Factory Method
You want to save system resources by reusing existing objects instead of rebuilding them each time
Factory Method
Your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products—they might be unknown beforehand or you simply want to allow for future extensibility
Abstract Factory
You have a class with a set of Factory Methods that blur its primary responsibility
Abstract Factory
You want to parametrize objects with operations
Command
You want to queue operations, schedule their execution, or execute them remotely
Command
You want to implement reversible operations
Command
You want to use some existing class, but its interface isn’t compatible with the rest of your code
Adapter
You want to reuse several existing subclasses that lack some common functionality that can’t be added to the superclass
Adapter
You need to have a limited but straightforward interface to a complex subsystem
Fasade
You want to structure a subsystem into layers
Fasade
You have a heavyweight service object that wastes system resources by being always up, even though you only need it from time to time - lazy initialization
Proxy
You want only specific clients to be able to use the service object; for instance, when your objects are crucial parts of an operating system and clients are various launched applications (including malicious ones)
Proxy
Local execution of a remote service. This is when the service object is located on a remote server
Proxy