How to use Design Patterns - Quiz Explanation
The answers you selected are indicated below, along with text that explains the correct answers.
1.
Suppose you need to define parallel class hierarchies of graphical user interface components for different platforms. Which of the following patterns is likely to be most helpful in that task?
Please select the best answer.
A.
Abstract Factory
B.
Decorator
C.
Façade
D.
Flyweight
2.
Suppose you have a back-end data model that must be connected to multiple front-end user interfaces. Which of these patterns would best assist you with keeping them all in sync?
Please select the best answer.
A.
Abstract Factory
B.
Mediator
C.
Factory Method
D.
Flyweight
The correct answer is B.
Mediator is one of the oldest patterns, and one of its many purposes is to connect front and back ends while allowing each to vary independently of the other. The Observer pattern is also a possible solution, but it isn't listed as a choice here.
3.
You need to leave decisions about which class to instantiate to the subclasses of an abstract base class. Which pattern best assists in this task?
Please select the best answer.
A.
Factory Method
B.
Flyweight
C.
Observer
D.
Mediator
The correct answer is A.
Instantiation is a task for a creational pattern. This rules out Flyweight, Observer, and Mediator. This is almost exactly a description of the purpose of the Factory Method pattern.
4.
You need to establish flexible one-to-many connections between objects so when the one object's state changes the many objects all become aware of the change.
Please select the best answer.
A.
Abstract Factory
B.
Builder
C.
Factory Method
D.
Observer
The correct answer is D.
Mediator would be another choice. However, Mediator is mostly responsible for many-to-many connections. Observer is more flexible and extensible than Mediator, though it only supports one-to-many connections.