Default Flashcards
Abstract Factory (Object Creational)
Book Definition: Provide an interface for families of related/dependent objects without specifying any concrete classes.
Basically, an interface of a factory. The concrete implementations of this interface each correspond to a “family” of concrete classes, which have differing implementations but common APIs.
Example: Dark Theme vs Light Theme UI Components could come from an “Abstract UI Component Factory” whose implementations are a Dark UI Factory and a Light UI Factory.
Builder (Object Creational)
Book Definition: Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Define an interface for parts (components) of a bigger component, where the bigger component can be built solely by calling the api of the interface, even though that produces heterogenous components.
Example: this is literally how React works.
Factory Method (Class Creational)
Book Definition: Define an interface for creating an object, but let subclasses decide which class that matches the interface to instantiate.
This is the “normal” factory pattern: parent class doesn’t want/need to know the details of its subclasses or wants to let them pick, so you define an interface, let it call an arbitrary factory which makes instances if said interface, and run with whatever it gets back
Example: a text editor reads a document but doesn’t know input type, uses a Document Reader Factory
Prototype (Object Creational)
Book Definition: Specify the kings of objects to create by using a prototypical instance, and create new objects by copying the prototype.
If you need to be able to make new instances of an object but don’t want to take a dependency, or you don’t want to use a factory but still need copies of an implementation of a subclass, take an instance (not a compile dependency; an actual object) and clone it.
it’s most useful in languages like C++ where a class isnt a manipulable 1st class object. X.prototype in JS is maybe not there clearest example (since the prototype “chain” in pure prototyping would only be link long) but it does serve as an example.
Singleton (Object Creational)
Book Definition: Ensure a class has only one instance and provide a global point of access to it.
If you want there to be only one of something, like a directory or registry of some kind, use a singleton; either literally export an instance of the thing, or a method that returns the same thing every time.
Example: an event manager (like a pub sub dispatcher), where you want all messages to go through the same single instance.
Adapter (Structural)
Book Definition: Convert the interface of a class into another interface that clients are expecting.
This is fairly obvious from the name.
Example: Consider a JSONtoXML adapter, or every physical electric plug in real life when traveling.
Bridge (Structural)
Book definition: Decouple an abstraction from its implementation so they can vary independently.
A bridge is basically an api contract that lives inside a project instead of across network. It lets the “api” implementations and the client/calling code (“abstraction” in the books term) be developed on and changed simultaneously, as long as their contract isn’t broken.
Example: the buttons on a remote vs. their actions in an abstract device.
Composite (Structural)
Book Definition: Compose objects into tree structures that represent “parts of a whole” hierarchies; treat each tree as if it were a single flat object.
Only makes sense when abstraction can be represented naturally as a tree; build a base interface that every element in the tree can fulfill; then use basic tree manipulation to do things.
Example: representing an organization with a strict hierarchy.
Decorator (Structural)
Book Definition: Attach additional responsibility to an object dynamically as an alternative to subclassing.
Build a wrapper around an object that implements all its methods but maybe enhances a few; from a consumer perspective these objects are the same type, allowing behavior to be extended—and decorators can even be nested!
Facade (Structural)
Book Definition: Provide a unified higher-level interface to a set of sub-interfaces within a subsystem.
A facade is a high level controller that can contain logic for calling parts of its subsystem to hide their grittier details. Somethings like GCD or a pub-sub system, a la a local version of a central api controller in a network system, is a good example.
Flyweight (Structural)
Book Definition: Use sharing to support large numbers of fine-grained objects efficiently.
If you have many similar objects in a system (especially if those similar parts are the expensive ones), use a flyweight (a “not quite singleton” that all the similar objects can point to) to reduce memory burden substantially.
Example: objects with identical graphics but different state in a ui intensively program like a video game.
Proxy (Structural)
Book Definition: Provide a surrogate or placeholder for another object in order to control access to that object.
A proxy can be thought of as a “singleton wrapper” - not necessarily strictly singleton, but one per wrapped resource-and since those resources are usually few. Like a decorator, a proxy injects behavior before or after calls, but usually that behavior is stuff like access validations, lock management, etc.
Chain of Responsibility (Behavioral)
Book Definition: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request, passing the request along until it is handled or finally ignored.
A chain of responsibility is a linked list of handlers which can either (pass a request on without operating on it, or operate on it and then drop it) or (operate on it before passing it on or drop it if it is no longer relevant).
Command (Behavioral)
Book Definition: Encapsulate a request as an object, allowing you to parameterize clients for different kinds of requests, queue/log requests, and support operations that can be undone or abandoned.
Represent an action as an object that can be constructed wherever that action can be fired, allowing you to separate the logic of the action from its construction, plus allowing you to operate on those requests as objects for additional logic.
Iterator (Behavioral)
Book Definition: Provide a way to access elements of sb aggregate object sequentially without exposing underlying representation.
An iterator lets you go through a collection linearly without needing to care if the collections is a tree, a list, etc. A data structure can have multiple iterators (depth first, breadth first for trees), but having a function take an iterator as input allows you to avoid caring about the implementation of the data structure.