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.