Default Flashcards

1
Q

Abstract Factory (Object Creational)

A

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.

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

Builder (Object Creational)

A

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.

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

Factory Method (Class Creational)

A

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

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

Prototype (Object Creational)

A

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.

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

Singleton (Object Creational)

A

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.

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

Adapter (Structural)

A

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.

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

Bridge (Structural)

A

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.

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

Composite (Structural)

A

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.

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

Decorator (Structural)

A

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!

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

Facade (Structural)

A

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.

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

Flyweight (Structural)

A

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.

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

Proxy (Structural)

A

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.

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

Chain of Responsibility (Behavioral)

A

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

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

Command (Behavioral)

A

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.

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

Iterator (Behavioral)

A

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.

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

Mediator (Behavioral)

A

Book Definition: Define an object that encapsulates how a set of objects interact. Mediators promote loose coupling by keeping objects from referring to each other explicitly, and lets you vary their actions independently.

Also called a “director” pattern in some langs.

17
Q

Memento (Behavioral)

A

Book Definition: Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.

A memento is an object that contains/is a copy of another object’s internal state at a moment in time. They’re used to make actions reversible, such as an undo history stack. A naive implementation is to just deep-clone an object to a new pointer/reference.

18
Q

Observer (Behavioral)

A

This is pub-sub.

19
Q

State (Behavioral)

A

Book Definition: Allow an object to alter its behavior when internal state changes. The object will appear to change its class.

Weird book definition; this pattern means “define an interface for the different states and for each possible state make a concrete implementation – then have the parent contain a member whose type is the interface.

20
Q

Strategy (Behavioral)

A

Book Definition: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary to independently from the clients that are using it.

Strategy is the OO way of passing a pointer to a function when your language doesn’t support lambdas or anonymous functions or things like that. You make classes that are jus one function definition in a way that fulfills s common interface.

21
Q

Template Method (Behavioral)

A

Book Definition: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

Make an abstract class with a main algorithm (the template method) that calls other abstract helper methods. Concrete implementations can then tweak the select helpers, but not the overall algorithm.

22
Q

Visitor (Behavioral)

A

Book Definition: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Have elements implement an “accept” method which accepts an interface “Visitor”. The visitor exposes methods for all possible element types, and the element’s “accept” is responsible for calling the appropriate version of the visitor’s “visit”. (eg ElementA’s accept() calls VisitA on the passed in visitor. This gets VisitA called in the scope of ElementA without including the logic inside it.