Design Patterns Flashcards

1
Q

You and your colleagues were tasked with creating a database application. Each member of the team was assigned a different operation on the database to implement. Your task is to implement the redo operation for a database object which may be performed multiple times.
Identify the design pattern

A

Memento

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

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

A

Memento

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

Various sorting algorithms have steps in common. Among the differences between the algorithms are where they start and stop comparing elements in an array as well as whether or not they will swap two elements under scrutiny to provide an array sorted in ascending or descending order, dependant on a compare operation. A particular algorithm will know how to implement its differences from other algorithms.
Identify the design pattern

A

Template Method

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

Define the skeleton of an algorithm
in an operation, deferring some steps to
subclasses. Lets subclasses
redefine certain steps of an algorithm
without changing the algorithm’s structure.”

A

Template Method

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

Lets a class defer instantiation to a subclass.

A

Factory Method

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

Defines an interface for creating an object, but lets subclasses decide which class to instantiate. This pattern lets a class defer instantiation to subclasses.

A

Factory Method

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

A famous actress needs a number of different outfits, each suited to a particular occasion:
Casual wear for activities in public. A personal stylist who follows fashion trends closely is responsible for this type of outfit.
A costume to wear while shooting movies. The costume department on a movie set is responsible for producing the correct costume(s). Formal evening dresses to wear to awards ceremonies and movie premiers. A fashion designer takes responsibility for producing an evening dress.
Assume you are tasked with producing an application that simulates an actress obtaining different types of outfits.
Identify the design pattern:

A

Factory Method

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

Which pattern is best described by the statement:
Creates an object by making a copy of an existing object

A

Prototype

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

A new strategy game is currently under development. The developer has created two classes representing units, namely Ornithopter and Devastator. Both of these classes inherit from an abstract class Unit. Having this arrangement will not only make it easy to handle units uniformly throughout the code, but will also allow for the easy addition of more units, like for example, if an expansion pack is created for the game at a later date. There is also a class named ConstructionYard which is able to create units of any type upon request. This class encapsulates two objects, one of type Ornithopter and one of type Devastator.
Identify the design pattern:

A

Prototype

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

Cloning a large army of Storm Troopers in the Star Wars Battlefront video game.

A

Prototype

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

Determine whether the following statement is True/False.
The prototype and prototype manager classes are always members of the Prototype design pattern.

A

False

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

Is the following statement True/False?

The Abstract Factory exhibits a one-to-one relationship between the factory and the product.

A

False

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

Darth Vader has commanded you to oversee the creation of an army for the new Death Star. 50% of the army should consist of Stormtroopers (identical soldiers cloned from the villain Jango Fett). 30% should be Battle Driods. 10% Special Assassins and 10% Sith Troopers. Darth Vader also made it clear that he wants to extend his army with different soldiers at a later stage.

Which design pattern/s would be most appropriate for creating a huge army according to Darth Vader’s specifications?
Abstract Factory
Prototype
Memento
Strategy

A

Abstract Factory
Prototype

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

Produces products with a similar theme.

A

Abstract Factory

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

Identify the design pattern that is used in situations where it is necessary to dynamically swap out algorithms.

A

Strategy

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

During the sorting of a numbered array, let subclasses execute steps or the entire sorting algorithm. Identify the pattern(s).
A. Factory Method
B. Strategy
C. Template Method
D. State

A

B. Strategy
C. Template Method

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

The implementation consists of a variation of classes for implementing different algorithms to solve a given problem and clients need to couple individually with these classes

A

Strategy

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

Identify the pattern that is best used for the following scenario.

You are designing an image processing program. The basic program should only be able to export images in the bmp and jpeg formats. Your client has however indicated that they want to add more file formats in later updates.

A

Strategy

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

Is the following statement True or False?

A key characteristic of the State and Strategy design patterns is the aggregational relationship.

A

True

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

Allow an object to alter its behaviour when its internal state changes. The object appears to change its class.

A

State

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

Identify the pattern that is best used for the following scenario.

A vending machine waits for a user to make a choice of the item they would like to purchase. Once a choice has been made, it will dispense the selected item only if it has stock and after the user has placed the correct coinage into the machine.

A

State

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

Compose objects into tree structures to
represent part-whole hierarchies. Lets clients treat individual objects and
compositions of objects uniformly.

A

Composite

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

Attach additional responsibilities to an object
dynamically. Provide a flexible
alternative to subclassing for extending
functionality.

A

Decorator

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

Routers typically have very slow processors and code has to be written efficiently to avoid overloading. Each time a new node is connected, the router creates an object in memory containing a lot of information that is exactly the same for all newly connected nodes. Only after initialization, the nodes change certain information in this object, such as the IP address. Which design pattern can be used to reduce computational time by avoiding the creation of the large object from scratch each time a new node is connected?

A

Prototype

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

Executing a function if a button in a GUI was clicked.

A

Observer

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

Define a one-to-many dependency
between objects so that when one object
changes state, all its dependents are notied
and updated automatically

A

Observer

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

In which participant of the Observer design pattern would the Iterator design pattern find the best use?
-The Iterator pattern doesn’t fit in with the Observer pattern.
-It depends if you also use the State pattern or not.
-ObserverIterator
-Subject
-Client
-Observer

A

Subject

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

Traversing over a list or map and accessing the elements sequentially.

A

Iterator

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

Sequentially access elements of an object.

A

Iterator

30
Q

This pattern is used extensively by C++ containers.

A

Iterator

31
Q

Provide a way to access the
elements of an aggregate object sequentially
without exposing it’s underlying
representation

A

Iterator

32
Q

Has a one-to-many relationship with Colleague participants.

A

Mediator

33
Q

DeFIne an object that encapsulates how a
set of objects interact. Promotes
loose coupling by keeping objects from referring to each other explicitly, and it lets
you vary the interaction independently.”

A

Mediator

34
Q

This pattern encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

A

Command

35
Q

Only select those that apply. The purpose of the Command design pattern is to:
- Capture and externalize an object’s internal state
- Handle undo and redo operations
- Parameterizing clients by passing requests encapsulated as objects

A
  • Handle undo and redo operations
  • Parameterizing clients by passing requests encapsulated as objects
36
Q

Provide a single design pattern that is most suitable in the given scenario.

A request is encapsulated as an object and then executed on a receiving object.

A

Command

37
Q

Due to the license issues with Macrosoft Office, you decide to write your own open-source document editor. To get things started, you decide to implement three of the most used functions, namely bold, italics, and underline. These functions must be encapsulated in their own classes, all inheriting from a parent class Markup. You already implemented a class called Document that contains all the text and markup, and a class SystemInput which receives mouse and keyboard events from the operating system. Another class called Manager, will be responsible for creating the markup functionality and making sure it is executed on the currently opened document. You need a generic structure that allows the easy addition of new markup functions at a later stage.

i. Identify the design pattern:

A

Command

38
Q

The Command design pattern improves efficiency of code by increasing coupling. True or False

A

False

39
Q

Requests can be sent as objects across the system. Which design pattern best suits this description?

A

Command

40
Q

Used to parametrize objects by an action that is to be performed.

A

Command

41
Q

When properly designed, the Command participant in the Command design pattern will implement the actual algorithm or action. True or False

A

False

42
Q

You want to create a system that supports operations which are undoable. However, instead of saving the complete system state every time, you want to maintain a list of performed operations that can be reversed. Which design pattern(s) do you need for this?

A

Mediator and Command

43
Q

You decided to use the Composite design pattern to model the new menu structure for the new MacroSoft OS. You have assigned short-cuts to the some of the nested menu options. You however would like to keep statistics of how many times a user clicks through the menu options and how many times the user makes use of the corresponding short-cut. Which pattern would you use to encapsulate the subtree defined in the composite as a short-cut?

A

Command

44
Q

Which one of the following is not a participant in the Command pattern?
Invoker
ConcreteReceiver
ConcreteCommand
Command
Receiver

A

ConcreteReceiver

45
Q

Encapsulate a request as an object, thereby
letting you parameterise clients with dierent
requests, queue or log requests, and support
undoable operations.

A

Command

46
Q

Convert an interface of a class into another interface clients expect.

A

Adapter

47
Q

An interface is converted into one that is required by the client by this pattern.

A

Adapter

48
Q

Avoid coupling the sender of a
request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

A

Chain of Responsibility

49
Q

The Chain of Responsibly is NOT implemented for the following reason.

A

Executing an algorithm step-by-step, where each step is added to the chain.

50
Q

Provide a unified interface to a set of interfaces in a subsystem.

A

Façade

51
Q

Decouples subsystems from each other.

A

Façade

52
Q

This pattern exhibits unidirectional behaviour unlike the mediator which exhibits multidirectional behaviour.

A

Façade

53
Q

A centralised interface that delegates requests to other classes or subsystems.

A

Facade

54
Q

The algorithm for creating a complex subsystem is independent of the parts that make up the subsystem and how the parts are assembled.

A

Builder

55
Q

Creates composite/complex objects in steps.

A

Builder

56
Q

True or False?

The Builder pattern allows one to vary a product’s internal representation.

A

True

57
Q

Defines a set of rules, a language capable of specifying the rules, and a grammar for defining the language, and an “engine” for parsing the grammar.

A

Interpreter

58
Q

The choice of implementation is configurable at run-time.

A

Bridge

59
Q

The application of the design pattern results in two orthogonal class hierarchies that can vary independently.

A

Bridge

60
Q

The Class Adapter design pattern uses delegation as its strategy.
True or False

A

False, Class Adapter uses private inheritance.

61
Q

These pattern(s) couple senders and receivers

A

Observer
Mediator
Command
Chain of Responsibility

62
Q

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

A

Visitor

63
Q

Provides a way of separating an algorithm from an object structure its operating on.

A

Visitor

64
Q

Provides a surrogate or placeholder for another object to control access to it.

A

Proxy

65
Q

Can hide the fact that a given object is in a different address space.

A

Proxy

66
Q

Ensure a class has only one instance and provide a global point of access to it.

A

Singleton

67
Q

Applies information hiding to ensure that its constructors are used in a strictly controlled manner.

A

Singleton

68
Q

Contains both an intrinsic and extrinsic state.

A

Flyweight

69
Q

A pattern where the attributes of an object instance are shared in order to save memory and the differences are dependent on the context.

A

Flyweight

70
Q

Accessing an object from a pool of objects using an ID.

A

Flyweight