What Type of Design Pattern Am I? Flashcards
Abstract Factory
Creational
_____________________
“The Factory of Factories”
Lets you produce a family of related products w/o specifying their concrete classes.
Concrete factories inherit from abstract factory and instantiate a concrete familial product (inherits from abstract product).
_____________________
Example: Abstract Furniture Factory Concrete Traditional Factory Concrete Modern Factory Concrete Couch Product Concrete Bed Product
Builder
Creational
_____________________
Solves Telescoping Constructor Anti-Pattern
Builds complex objects by separating the common CONSTRUCTION steps (abstract Builder) from the various possible REPRESENTATION - aka concrete Builders. These concrete Builders implement the same steps in a different manner, instead of constructor overloading.
Optional director to have a reusable definition for the order of the construction methods, or you can call from client code.
_____________________
Example: Director Abstract Car Builder Concrete Manual Car Builder Concrete Automatic Car Builder
Factory Method
Creational
_____________________
Creates an interface for creating objects, but allows subclasses to determine which class to instantiate. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Example: Abstract Travel Abstract Transport Concrete Ship Concrete Plane Concrete WaterTravel (creates Ship) Concrete AerialTravel (creates Plane)
Prototype
Creational
_____________________
By having a copy() method in your class (making it a Prototype class), you can create exact copies easily.
_____________________
In Python, simply use:
import copy
prototype = Prototype(specific attrs)
copy_1 = copy.deepcopy(prototype)
Singleton
Creational
_____________________
Restrict a class to one instance. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Borg implementation - for any class that inherits from Borg, instances retain the same shared state.
In Python, simply define code in a module instead of a class as only one copy is imported
class Borg: _shared_state = {} def \_\_init\_\_(self): self.\_\_dict\_\_ = self._shared_state
class Single(Borg):
def __init__(self, **kwargs):
Borg.__init__(self)
self._shared_state.update(kwargs)
def __str__(self):
return ‘{}’.format(self._shared_state)
Adapter
Structural
_____________________
Allows objects with incompatible interfaces to collaborate.
_____________________
Wrap one interface with an adapter that is compatible with the client/other interface, which handles the conversion.
Bridge
Structural
_____________________
Separate an abstraction from its implementation so the two can vary independently.
_____________________
Take a Shape abstraction that has subclasses Rectangle, Square, etc.
As you add other dimensions - color, size, etc. - and other kinds of shapes - triangles, circles, etc. - your subclasses will become exponential if you create one for each permutation.
Instead allow the varied implementations - different kinds of shapes - to vary independently of their abstraction - the shape class - by creating bridges from the abstraction to classes that define features as a field reference.
Basic delegation/has-a logic in the abstract interface.
Composite
Structural
_____________________
Composes objects into tree structures, and treats individual and composite objects uniformly.
_____________________
Like a directory is-a directory that has-a set of files, any of which can be a directory.
Decorator
Structural
_____________________
Add functionality to a function/class by wrapping it with a decorator.
In Python, a callable that returns a callable.
_____________________
@ my_decorator def my_fxn(): pass
my_fxn = my_decorator(my_fxn) => what @ does
def my_decorator(func): @wraps(func) # inspection shows func, not wrap def wrapper(*args, **kwargs): # decorate away return func(*args, **kwargs) return wrapper
Facade
Structural
_____________________
A simplified interface for a more complex object/interface/library.
When you need limited functionality from the more complex source
Flyweight
Structural
_____________________
A flyweight object has a shared intrinsic state across instances (constant data), and only needs to differ by the extrinsic state (changing data).
This speeds performance and saves memory.
Proxy
Structural
_____________________
A placeholder for an object to control access to the potentially resource-heavy object, and only as needed.
A proxy has-a service, and they share an interface.
_____________________
A credit card is a proxy for a bank account/cash. They have the same interface - make payments. But for both the client and server it is safer/easier than cash.
Chain of Responsibility
Behavioral
_____________________
A request is sent through a linked list of handlers with recursive traversal: a handler either handles the request if they can, or passes to the next handler if they cannot.
Command
Behavioral
_____________________
Encapsulates a request as an object, separating the requestor from the service that performs the request.
_____________________
A restaurant order (check) represents a command - it is an object that represents the customer’s specific request for the cook to make.
Interpreter
Behavioral
_____________________
Implements a specialized language.
Translating source code into an abstract syntax tree, a composite tree structure representing the code with
- operations stored as compound nodes of the tree that hold the grammar functionality
- and terminal nodes that represent operands/variables
Often a parser is used to traverse the tree.
_____________________
The Python Interpreter uses this pattern.
Iterator
Behavioral
_____________________
Provide a way to traverse elements of an aggregate object without exposing the underlying representation
_____________________
In Python, not really necessary as iterable objects create iterators when they go through iteration contexts (loops, comprehensions)
And if you’re defining a class, you can use operator overloading on the __iter__ and __next__ methods to define the iteration context. (Iter returns the iterable, next returns the next, raise StopIteration to end the iteration context).
Mediator
Behavioral
_____________________
Define an object that controls how a set of objects interact, so that they do not interact directly, but always through the mediator.
_____________________
We want to design reusable components without complicated dependencies between one another.
Single responsibility principle - extract the component communication to a single space, making it easier to comprehend and maintain
Open/closed principle - you can introduce new mediators w/o altering the components
Memento
Behavioral
_____________________
A way to restore an object to a previous state w/o breaking encapsulation.
Using a snapshot object that stores the state and only the originator object accesses, and a caretaker that knows how and when the originator should restore to a previous state that stores a stack of mementos.
Observer
Behavioral
_____________________
Design a subscription mechanism that notifies subscribers when a change is made by the object they observe.
The observer will have a subscriber list that subscribers can unsubscribe themselves from, and a notify method that loops over the subscriber list and calls their update methods appropriately.
State
Behavioral
_____________________
Allow an object to alter its behavior when its internal state changes by storing a state object in the original context object that defines all state-specific behavior
This is identical in implementation to a Bridge, but the purpose is not to decouple abstraction from implementation, but to alter the object’s behavior.
Similarly, similar in form to Strategy, but with different intent.
_____________________
Your phone’s state is unlocked, so pressing buttons executes different functions.
If your phone’s state is unlocked, you are prompted to unlock the phone.
Strategy
Behavioral
_____________________
Define a family of algorithms, encapsulate each one, and make them interchangeable.
Store a strategy object in the original context object that houses the preferred methodology.
Basically the same as State in design (and therefore Bridge), but differ in intent.
_____________________
You have to get to the airport, but depending on factors such as time and money, you might choose a different strategy: walking, bus, driving.
Template Method
Behavioral
_____________________
Create an abstract template interface that represents the skeleton of an algorithm, that concrete subclasses will inherit and overload the steps as necessary.
Related to Strategy in intent, both about algorithms.
- Template Method uses inheritance to change part of the algorithm and the logic of an entire class
- Strategy uses delegation to change the an entire algorithm and the logic of a single object.
Visitor
Behavioral
_____________________
Abstract functionality/an operation to be performed on the elements of an object structure/aggregate hierarchy (often tree).
Like a taxi company might have an object structure of customers, and dispatch a taxi (visitor) that will perform the operation, instead of the customer driving themselves.