Design Patterns Flashcards
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
Memento
Without violating encapsulation, capture and externalise an object’s internal state so that the object can be restored to this state later.
Memento
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
Template Method
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.”
Template Method
Lets a class defer instantiation to a subclass.
Factory Method
Defines an interface for creating an object, but lets subclasses decide which class to instantiate. This pattern lets a class defer instantiation to subclasses.
Factory Method
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:
Factory Method
Which pattern is best described by the statement:
Creates an object by making a copy of an existing object
Prototype
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:
Prototype
Cloning a large army of Storm Troopers in the Star Wars Battlefront video game.
Prototype
Determine whether the following statement is True/False.
The prototype and prototype manager classes are always members of the Prototype design pattern.
False
Is the following statement True/False?
The Abstract Factory exhibits a one-to-one relationship between the factory and the product.
False
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
Abstract Factory
Prototype
Produces products with a similar theme.
Abstract Factory
Identify the design pattern that is used in situations where it is necessary to dynamically swap out algorithms.
Strategy
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
B. Strategy
C. Template Method
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
Strategy
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.
Strategy
Is the following statement True or False?
A key characteristic of the State and Strategy design patterns is the aggregational relationship.
True
Allow an object to alter its behaviour when its internal state changes. The object appears to change its class.
State
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.
State
Compose objects into tree structures to
represent part-whole hierarchies. Lets clients treat individual objects and
compositions of objects uniformly.
Composite
Attach additional responsibilities to an object
dynamically. Provide a flexible
alternative to subclassing for extending
functionality.
Decorator
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?
Prototype
Executing a function if a button in a GUI was clicked.
Observer
Define a one-to-many dependency
between objects so that when one object
changes state, all its dependents are notied
and updated automatically
Observer
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
Subject
Traversing over a list or map and accessing the elements sequentially.
Iterator
Sequentially access elements of an object.
Iterator
This pattern is used extensively by C++ containers.
Iterator
Provide a way to access the
elements of an aggregate object sequentially
without exposing it’s underlying
representation
Iterator
Has a one-to-many relationship with Colleague participants.
Mediator
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.”
Mediator
This pattern encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Command
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
- Handle undo and redo operations
- Parameterizing clients by passing requests encapsulated as objects
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.
Command
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:
Command
The Command design pattern improves efficiency of code by increasing coupling. True or False
False
Requests can be sent as objects across the system. Which design pattern best suits this description?
Command
Used to parametrize objects by an action that is to be performed.
Command
When properly designed, the Command participant in the Command design pattern will implement the actual algorithm or action. True or False
False
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?
Mediator and Command
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?
Command
Which one of the following is not a participant in the Command pattern?
Invoker
ConcreteReceiver
ConcreteCommand
Command
Receiver
ConcreteReceiver
Encapsulate a request as an object, thereby
letting you parameterise clients with dierent
requests, queue or log requests, and support
undoable operations.
Command
Convert an interface of a class into another interface clients expect.
Adapter
An interface is converted into one that is required by the client by this pattern.
Adapter
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.
Chain of Responsibility
The Chain of Responsibly is NOT implemented for the following reason.
Executing an algorithm step-by-step, where each step is added to the chain.
Provide a unified interface to a set of interfaces in a subsystem.
Façade
Decouples subsystems from each other.
Façade
This pattern exhibits unidirectional behaviour unlike the mediator which exhibits multidirectional behaviour.
Façade
A centralised interface that delegates requests to other classes or subsystems.
Facade
The algorithm for creating a complex subsystem is independent of the parts that make up the subsystem and how the parts are assembled.
Builder
Creates composite/complex objects in steps.
Builder
True or False?
The Builder pattern allows one to vary a product’s internal representation.
True
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.
Interpreter
The choice of implementation is configurable at run-time.
Bridge
The application of the design pattern results in two orthogonal class hierarchies that can vary independently.
Bridge
The Class Adapter design pattern uses delegation as its strategy.
True or False
False, Class Adapter uses private inheritance.
These pattern(s) couple senders and receivers
Observer
Mediator
Command
Chain of Responsibility
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.
Visitor
Provides a way of separating an algorithm from an object structure its operating on.
Visitor
Provides a surrogate or placeholder for another object to control access to it.
Proxy
Can hide the fact that a given object is in a different address space.
Proxy
Ensure a class has only one instance and provide a global point of access to it.
Singleton
Applies information hiding to ensure that its constructors are used in a strictly controlled manner.
Singleton
Contains both an intrinsic and extrinsic state.
Flyweight
A pattern where the attributes of an object instance are shared in order to save memory and the differences are dependent on the context.
Flyweight
Accessing an object from a pool of objects using an ID.
Flyweight