Free Design Pattern Trivia Question and Answers — Questions and Answers
Question 1: Which Design Pattern should you employ when... <br> a class must only have one instance and be reachable by clients via a well-known access point?
- Factory Method
- Bridge
- Singleton (Correct answer)
- Prototype
Correct answer: Singleton
The Singleton design pattern ensures that a class has only one instance and provides a global point of access to that instance. This is essential when exactly one object is needed to coordinate actions across the system, such as a logger, configuration manager, or a database connection pool. It guarantees a single, consistent state for that specific resource.
Question 2: Which Design Pattern ought to be applied when a large number of related classes merely differ in behavior or when you require several algorithmic variations? You may specify algorithms that represent various space/time trade-offs, for instance.
- Factory Method
- Strategy (Correct answer)
- Composite
- Proxy
Correct answer: Strategy
The Strategy design pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows the algorithm to vary independently from clients that use it. This pattern is ideal when you have multiple ways to perform a specific task (e.g., different sorting algorithms or payment methods) and need to switch between them dynamically at runtime without altering client code.
Question 3: Which Design Pattern should you employ when... <br> there is a language to parse and abstract syntax trees can be used to express assertions in the language?
- Singleton
- Facade
- Composite
- Interpreter (Correct answer)
Correct answer: Interpreter
The Interpreter design pattern is used to define a grammatical representation for a language and provide an interpreter to deal with this grammar. It's particularly useful when you need to parse and evaluate expressions in a domain-specific language (DSL), where each rule in the grammar can be represented as a class, and an abstract syntax tree can be built to represent sentences in the language.
Question 4: Which Design Pattern ought to be applied when... <br> to manage extensions of subclasses. You can create a template method that only allows extensions at certain points and calls "hook" operations elsewhere.
- Factory Method
- Template Method (Correct answer)
- State
- Strategy
Correct answer: Template Method
The Template Method pattern defines the skeleton of an algorithm in a base class, deferring some steps to subclasses. It allows subclasses to redefine certain parts of an algorithm without changing its overall structure. The 'hook' operations mentioned are specific points where subclasses can optionally extend or modify behavior.
Question 5: Which Design Pattern should you use when a system should be independent of the creation, composition, and representation of its products? Also, which Design Pattern should you use when the classes to instantiate are determined at runtime, such as when dynamic loading is used?
- Facade
- Flyweight
- Prototype (Correct answer)
- Abstract Factory
Correct answer: Prototype
The Prototype pattern creates new objects by copying an existing object, known as the prototype. This approach makes the system independent of how its products are created, composed, and represented. It's particularly useful when classes to instantiate are determined at runtime, as you can clone the appropriate prototype instance dynamically.
Question 6: Which Design Pattern ought to be applied when... <br> an object structure comprises a variety of classes of objects, each with a different interface, and you want to conduct actions on these objects that rely on their concrete classes?
- Adapter
- Decorator
- Facade
- Visitor (Correct answer)
Correct answer: Visitor
The Visitor pattern allows you to define a new operation to be performed on elements of an object structure without changing the classes of the elements themselves. It separates the algorithm from the object structure, enabling new operations to be added easily across a hierarchy of objects, even if they have different interfaces, by 'visiting' each concrete class.
Question 7: Which Design Pattern ought to be applied when a group of objects interact in intricate yet clearly defined ways? The ensuing interdependencies are chaotic and challenging to comprehend.
- Mediator (Correct answer)
- Facade
- Factory Method
- Template Method
Correct answer: Mediator
The Mediator pattern defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly, thereby reducing the complex, chaotic interdependencies that can arise when many objects interact directly. This centralizes control over interactions, making them easier to understand and manage.
Which Design Pattern should you employ when...
a class must only have one instance and be reachable by clients via a well-known access point?