When to use Flashcards

1
Q

You want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime

A

Strategy

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

You have a lot of similar classes that only differ in the way they execute some behavior.

A

Strategy

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

You want isolate the business logic of a class from the implementation details of algorithms that may not be as important in the context of that logic.

A

Strategy

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

Your class has a massive conditional statement that switches between different variants of the same algorithm.

A

Strategy

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

Changes to the state of one object may require changing other objects, and the actual set of objects is unknown beforehand or changes dynamically.

A

Observer

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

Some objects in your app must observe others, but only for a limited time or in specific cases.

A

Observer

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

You need to be able to assign extra behaviors to objects at runtime without breaking the code that uses these objects.

A

Decorator

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

It’s awkward or not possible to extend an object’s behavior using inheritance.

A

Decorator

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

You don’t know beforehand the exact types and dependencies of the objects your code should work with.

A

Factory Method

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

You want to provide users of your library or framework with a way to extend its internal components

A

Factory Method

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

You want to save system resources by reusing existing objects instead of rebuilding them each time

A

Factory Method

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

Your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products—they might be unknown beforehand or you simply want to allow for future extensibility

A

Abstract Factory

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

You have a class with a set of Factory Methods that blur its primary responsibility

A

Abstract Factory

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

You want to parametrize objects with operations

A

Command

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

You want to queue operations, schedule their execution, or execute them remotely

A

Command

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

You want to implement reversible operations

A

Command

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

You want to use some existing class, but its interface isn’t compatible with the rest of your code

A

Adapter

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

You want to reuse several existing subclasses that lack some common functionality that can’t be added to the superclass

A

Adapter

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

You need to have a limited but straightforward interface to a complex subsystem

A

Fasade

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

You want to structure a subsystem into layers

A

Fasade

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

You have a heavyweight service object that wastes system resources by being always up, even though you only need it from time to time - lazy initialization

A

Proxy

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

You want only specific clients to be able to use the service object; for instance, when your objects are crucial parts of an operating system and clients are various launched applications (including malicious ones)

A

Proxy

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

Local execution of a remote service. This is when the service object is located on a remote server

A

Proxy

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

You want to keep a history of requests to the service object.

25
You need to cache results of client requests and manage the life cycle of this cache, especially if results are quite large
Proxy
26
You need to be able to dismiss a heavyweight object once there are no clients that use it
Proxy
27
You want to divide and organize a monolithic class that has several variants of some functionality (for example, if the class can work with various database servers)
Bridge
28
You need to extend a class in several orthogonal (independent) dimensions.
Bridge
29
You need to be able to switch implementations at runtime
Bridge, Strategy
30
You want to let clients extend only particular steps of an algorithm, but not the whole algorithm or its structure
Template Method
31
You have several classes that contain almost identical algorithms with some minor differences. As a result, you might need to modify all classes when the algorithm changes
Template Method
32
You have to implement a tree-like object structure
Composite
33
You want the client code to treat both simple and complex elements uniformly
Composite
34
Your collection has a complex data structure under the hood, but you want to hide its complexity from clients (either for convenience or security reasons).
Iterator
35
Your code to be able to traverse different data structures or when types of these structures are unknown beforehand.
Iterator
36
You have an object that behaves differently depending on its current state, the number of states is enormous, and the state-specific code changes frequently
State
37
You have a class polluted with massive conditionals that alter how the class behaves according to the current values of the class’s fields
State
38
You have a lot of duplicate code across similar states and transitions of a condition-based state machine
State
39
Use the this pattern to get rid of a “telescopic constructor” (constructor with several optional parameters)
Builder
40
Use this pattern when you want your code to be able to create different representations of some product (for example, stone and wooden houses)
Builder
41
Use this pattern to construct Composite trees or other complex objects
Builder
42
Use this pattern when your code shouldn’t depend on the concrete classes of objects that you need to copy
Prototype
43
Use the pattern when you want to reduce the number of subclasses that only differ in the way they initialize their respective objects. Somebody could have created these subclasses to be able to create objects with a specific configuration
Prototype
44
Use this pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program.
Singleton
45
Use this pattern when you need stricter control over global variables
Singleton
46
Use the this pattern only when your program must support a huge number of objects which barely fit into available RAM.
Flyweight
47
Use this pattern when your program is expected to process different kinds of requests in various ways, but the exact types of requests and their sequences are unknown beforehand
Chain of Responsibility
48
Use the pattern when it’s essential to execute several handlers in a particular order
Chain of Responsibility
49
Use the this pattern when the set of handlers and their order are supposed to change at runtime
Chain of Responsibility
50
Use this pattern when it’s hard to change some of the classes because they are tightly coupled to a bunch of other classes
Mediator
51
The pattern lets you extract all the relationships between classes into a separate class, isolating any changes to a specif- ic component from the rest of the components
Mediator
52
Use the pattern when you can’t reuse a component in a differ- ent program because it’s too dependent on other components
Mediator
53
Use the pattern when you find yourself creating tons of com- ponent subclasses just to reuse some basic behavior in various contexts
Mediator
54
Use the pattern when you want to produce snapshots of the object’s state to be able to restore a previous state of the object
Memento
55
Use the pattern when you need to deal with transactions (i.e., if you need to roll back an operation on error)
Memento
56
Use the patern when you need to perform an operation on all elements of a complex object structure (for example, an object tree).
Visitor
57
Use the pattern to clean up the business logic of auxiliary behaviors.
Visitor
58
Use the pattern when a behavior makes sense only in some classes of a class hierarchy, but not in others.
Visitor
59