What Type of Design Pattern Am I? Flashcards

1
Q

Abstract Factory

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Builder

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Factory Method

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Prototype

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Singleton

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Adapter

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Bridge

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Composite

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Decorator

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Facade

A

Structural
_____________________

A simplified interface for a more complex object/interface/library.

When you need limited functionality from the more complex source

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Flyweight

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Proxy

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Chain of Responsibility

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Command

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Interpreter

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Iterator

A

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).

17
Q

Mediator

A

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

18
Q

Memento

A

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.

19
Q

Observer

A

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.

20
Q

State

A

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.

21
Q

Strategy

A

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.

22
Q

Template Method

A

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.
23
Q

Visitor

A

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.