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.

A

Proxy

25
Q

You need to cache results of client requests and manage the life cycle of this cache, especially if results are quite large

A

Proxy

26
Q

You need to be able to dismiss a heavyweight object once there are no clients that use it

A

Proxy

27
Q

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)

A

Bridge

28
Q

You need to extend a class in several orthogonal (independent) dimensions.

A

Bridge

29
Q

You need to be able to switch implementations at runtime

A

Bridge, Strategy

30
Q

You want to let clients extend only particular steps of an algorithm, but not the whole algorithm or its structure

A

Template Method

31
Q

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

A

Template Method

32
Q

You have to implement a tree-like object structure

A

Composite

33
Q

You want the client code to treat both simple and complex elements uniformly

A

Composite

34
Q

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

A

Iterator

35
Q

Your code to be able to traverse different data structures or when types of these structures are unknown beforehand.

A

Iterator

36
Q

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

A

State

37
Q

You have a class polluted with massive conditionals that alter how the class behaves according to the current values of the class’s fields

A

State

38
Q

You have a lot of duplicate code across similar states and transitions of a condition-based state machine

A

State

39
Q

Use the this pattern to get rid of a “telescopic constructor” (constructor with several optional parameters)

A

Builder

40
Q

Use this pattern when you want your code to be able to create different representations of some product (for example, stone and wooden houses)

A

Builder

41
Q

Use this pattern to construct Composite trees or other complex objects

A

Builder

42
Q

Use this pattern when your code shouldn’t depend
on the concrete classes of objects that you need to copy

A

Prototype

43
Q

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

A

Prototype

44
Q

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.

A

Singleton

45
Q

Use this pattern when you need stricter control over
global variables

A

Singleton

46
Q

Use the this pattern only when your program must support a huge number of objects which barely fit into available RAM.

A

Flyweight

47
Q

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

A

Chain of Responsibility

48
Q

Use the pattern when it’s essential to execute several handlers
in a particular order

A

Chain of Responsibility

49
Q

Use the this pattern when the set of handlers and their order
are supposed to change at runtime

A

Chain of Responsibility

50
Q

Use this pattern when it’s hard to change some of the
classes because they are tightly coupled to a bunch of other
classes

A

Mediator

51
Q

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

A

Mediator

52
Q

Use the pattern when you can’t reuse a component in a differ-
ent program because it’s too dependent on other components

A

Mediator

53
Q

Use the pattern when you find yourself creating tons of com-
ponent subclasses just to reuse some basic behavior in various
contexts

A

Mediator

54
Q

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

A

Memento

55
Q

Use the pattern when you need to deal with transactions (i.e., if you need to roll back an operation
on error)

A

Memento

56
Q

Use the patern when you need to perform an operation on all elements of a complex object structure (for example, an object tree).

A

Visitor

57
Q

Use the pattern to clean up the business logic of auxiliary behaviors.

A

Visitor

58
Q

Use the pattern when a behavior makes sense only in some classes of a class hierarchy, but not in others.

A

Visitor

59
Q
A