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