1.
Which Design Pattern should you use when....
-
a system should be configured with one of multiple families of products.
Correct Answer
B. Abstract Factory
Explanation
The Abstract Factory design pattern should be used when a system needs to be configured with one of multiple families of products. This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows the client code to create objects of different types that belong to the same family, ensuring that the created objects are compatible and work together seamlessly.
2.
Which Design Pattern should you use when....
-
you want to use an existing class, and its interface does not match the one you need.
Correct Answer
A. Adapter
Explanation
The Adapter design pattern should be used when you want to use an existing class, but its interface does not match the one you need. The Adapter pattern allows you to create a new class that acts as a bridge between the existing class and the desired interface. This new class, known as the adapter, converts the interface of the existing class into the interface that is expected by the client. By using the Adapter pattern, you can reuse existing classes without modifying their code, thus promoting code reusability and flexibility.
3.
Which Design Pattern should you use when....
-
you want to avoid a permanent binding between an abstraction and its implementation. This might be the
case, for example, when the implementation must be selected or switched at run-time.
Correct Answer
C. Bridge
Explanation
The Bridge design pattern should be used when you want to avoid a permanent binding between an abstraction and its implementation. This is useful when the implementation needs to be selected or switched at runtime. The Bridge pattern allows the abstraction and implementation to vary independently, allowing for more flexibility and extensibility in the code. It separates the abstraction from its implementation by creating a bridge between them, allowing them to evolve independently. This pattern promotes loose coupling and allows for easier maintenance and modification of the code.
4.
Which Design Pattern should you use when....
-
the construction process must allow different representations for the object that's constructed.
Correct Answer
D. Builder
Explanation
The Builder design pattern should be used when the construction process must allow different representations for the object that's constructed. The Builder pattern separates the construction of an object from its representation, allowing the same construction process to create different representations. This pattern is useful when there are multiple ways to construct an object and when the construction process needs to be flexible and independent of the final representation.
5.
Which Design Pattern should you use when....
-
more than one object may handle a request, and the handler isn't known a priori. The handler
should be ascertained automatically.
Correct Answer
B. Chain of Responsibility
Explanation
The Chain of Responsibility design pattern should be used in a situation where more than one object may handle a request, and the handler is not known beforehand. This pattern allows multiple objects to have a chance to handle the request, with each object in the chain having the option to pass the request to the next object in the chain. The handler is ascertained automatically as the request travels through the chain until it is handled or reaches the end of the chain.
6.
Which Design Pattern should you use when....
-
you want to parameterize objects by an action to perform.
Correct Answer
A. Command
Explanation
The Command design pattern should be used when you want to parameterize objects by an action to perform. This pattern decouples the sender of a request from the receiver, allowing multiple requests to be handled by different receivers. The Command pattern encapsulates a request as an object, which can be passed as a parameter, stored, and executed at a later time. This provides flexibility and extensibility in handling different actions or operations.
7.
Which Design Pattern should you use when....
-
you want to represent part-whole hierarchies of objects.
Correct Answer
A. Composite
Explanation
The Composite design pattern should be used when you want to represent part-whole hierarchies of objects. This pattern allows you to treat individual objects and groups of objects in a uniform way, by creating a tree-like structure where both individual objects and groups of objects can be treated as a single object. This pattern is useful when you need to work with complex structures that can be composed of smaller parts, and you want to be able to perform operations on both the individual parts and the whole structure.
8.
Which Design Pattern should you use when....
-
you want to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
Correct Answer
C. Decorator
Explanation
The Decorator design pattern should be used when you want to add responsibilities to individual objects dynamically and transparently, without affecting other objects. The Decorator pattern allows you to add new behaviors or functionalities to an object by wrapping it with a decorator class. This way, you can add or remove responsibilities from an object at runtime, without modifying its structure or affecting other objects that use it.
9.
Which Design Pattern should you use when....
-
you want to provide a simple interface to a complex subsystem.
Correct Answer
B. Facade
Explanation
The Facade design pattern should be used when you want to provide a simple interface to a complex subsystem. This pattern provides a unified interface that hides the complexities of a subsystem and allows clients to interact with it in a simplified manner. By using the Facade pattern, clients can access the subsystem through a single interface, making it easier to use and understand.
10.
Which Design Pattern should you use when....
-
a class wants its subclasses to specify the objects it creates.
Correct Answer
D. Factory Method
Explanation
The Factory Method design pattern should be used when a class wants its subclasses to specify the objects it creates. This pattern allows a class to defer the instantiation of an object to its subclasses, allowing them to decide which concrete class to instantiate. This promotes loose coupling between the creator class and the objects it creates, as the creator class only depends on an abstract interface or base class.
11.
Which Design Pattern should you use when....
-
an application uses a large number of objects and the storage costs are high because of the sheer quantity of objects.
Correct Answer
C. Flyweight
Explanation
When an application uses a large number of objects and the storage costs are high because of the sheer quantity of objects, the Flyweight design pattern should be used. The Flyweight pattern aims to minimize memory usage by sharing common data between multiple objects. It achieves this by separating intrinsic state (shared among objects) from extrinsic state (unique to each object). By doing so, the Flyweight pattern reduces the memory footprint of the application and improves performance.
12.
Which Design Pattern should you use when....
-
there is a language to interpret, and you can represent statements in the language as abstract syntax trees.
Correct Answer
A. Interpreter
Explanation
The correct answer is Interpreter. The Interpreter design pattern is used when there is a language to interpret and you can represent statements in the language as abstract syntax trees. This pattern allows you to define a grammar for the language and provides a way to evaluate or interpret the language statements. It separates the parsing of the language statements from their execution, making it easier to add new expressions or modify the grammar of the language.
13.
Which Design Pattern should you use when....
-
you want to access an aggregate object's contents without exposing its internal representation.
Correct Answer
A. Iterator
Explanation
The Iterator design pattern should be used when you want to access an aggregate object's contents without exposing its internal representation. This pattern provides a way to access the elements of an aggregate object sequentially without exposing the underlying structure of the object. It decouples the client from the implementation details of the aggregate object, allowing for easy traversal and manipulation of the collection.
14.
Which Design Pattern should you use when....
-
a set of objects communicate in well-defined but complex ways. The resulting interdependencies
are unstructured and difficult to understand.
Correct Answer
D. Mediator
Explanation
The Mediator design pattern should be used when a set of objects communicate in well-defined but complex ways, and the resulting interdependencies are unstructured and difficult to understand. The Mediator pattern promotes loose coupling by encapsulating the communication logic between objects within a mediator object. This allows the objects to interact with each other indirectly through the mediator, reducing the direct dependencies between them and making the system more maintainable and easier to understand.
15.
Which Design Pattern should you use when....
-
a snapshot of (some portion of) an object's state must be saved so that it can be restored to that
state later, and a direct interface to obtaining the state would expose implementation details and break the object's encapsulation.
Correct Answer
B. Memento
Explanation
The Memento design pattern should be used in this scenario. The Memento pattern allows for the saving and restoring of an object's state without exposing its implementation details or breaking encapsulation. It provides a way to capture the internal state of an object and store it externally, so that it can be restored later if needed. This pattern is useful when there is a need to save and restore the state of an object, while keeping its implementation details hidden.
16.
Which Design Pattern should you use when....
-
When an object should be able to notify other objects without making assumptions about who these objects
are. In other words, you don't want these objects tightly coupled.
Correct Answer
C. Observer
Explanation
The Observer design pattern should be used when an object needs to notify other objects without making assumptions about who these objects are. This pattern allows for loose coupling between the objects, as the notifying object does not need to have direct knowledge of the receiving objects. Instead, the notifying object maintains a list of observers and notifies them when a specific event occurs. This allows for flexibility and extensibility in the system, as new observers can be easily added without modifying the notifying object.
17.
Which Design Pattern should you use when....
-
a system should be independent of how its products are created, composed, and represented; and
when the classes to instantiate are specified at run-time, for example, by dynamic loading
Correct Answer
A. Prototype
Explanation
The Prototype design pattern should be used when a system needs to be independent of how its products are created, composed, and represented. It allows objects to be created by cloning existing objects, rather than relying on explicit instantiation. This pattern is also useful when the classes to instantiate are specified at runtime, such as through dynamic loading.
18.
Which Design Pattern should you use when....
-
there is a need for a more versatile or sophisticated reference to an object than a simple pointer and wraps an object to control access to it.
Correct Answer
D. Proxy
Explanation
A Proxy design pattern is used when there is a need for a more versatile or sophisticated reference to an object than a simple pointer. It wraps an object to control access to it, acting as an intermediary between the client and the object. The Proxy pattern can be used to add additional functionality to the object, such as providing a level of security or caching. It allows for the implementation of lazy loading, where the actual object is created only when it is needed.
19.
Which Design Pattern should you use when....
-
there must be exactly one instance of a class, and it must be accessible to clients from a wellknown access point.
Correct Answer
B. Singleton
Explanation
The Singleton design pattern should be used when there needs to be exactly one instance of a class and it must be accessible to clients from a well-known access point. This pattern ensures that only one instance of the class is created and provides a global point of access to it. It is commonly used in scenarios where there should be only one instance of a database connection, a logger, or a configuration manager, for example.
20.
Which Design Pattern should you use when....
-
an object's behavior depends on its state, and it must change its behavior at run-time depending on that state.
Correct Answer
C. State
Explanation
The State design pattern should be used when an object's behavior depends on its state and it needs to change its behavior at runtime based on that state. This pattern allows an object to alter its behavior by changing its internal state, without changing its class. It encapsulates the different states as separate classes and allows the object to delegate the behavior to the current state class. This promotes flexibility and maintainability by separating the behavior logic from the object's class and making it easier to add or modify states in the future.
21.
Which Design Pattern should you use when....
-
many related classes differ only in their behavior or you need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs.
Correct Answer
D. Strategy
Explanation
The Strategy design pattern should be used when there are many related classes that differ only in their behavior or when different variants of an algorithm are needed. This pattern allows for the definition of different algorithms reflecting different space/time trade-offs. By encapsulating each algorithm in a separate class and allowing the client to choose the desired algorithm dynamically, the Strategy pattern promotes flexibility and maintainability in the codebase.
22.
Which Design Pattern should you use when....
-
to control subclasses extensions. You can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points.
Correct Answer
C. Template Method
Explanation
The Template Method design pattern should be used when you want to control subclasses extensions. This pattern allows you to define a template method that calls "hook" operations at specific points, which means that subclasses can only extend the functionality of the template method at those specific points. This provides a way to have a common structure or algorithm in the base class, while allowing subclasses to provide their own implementations for certain steps of the algorithm.
23.
Which Design Pattern should you use when....
-
an object structure contains many classes of objects with differing interfaces, and you want to perform
operations on these objects that depend on their concrete classes.
Correct Answer
C. Visitor
Explanation
The Visitor design pattern should be used in this scenario. The Visitor pattern allows you to define new operations on a group of classes without modifying their individual implementations. It is useful when you have a complex object structure with many classes that have different interfaces, and you want to perform operations on these objects that depend on their concrete classes. By using the Visitor pattern, you can separate the algorithm from the objects being operated on, resulting in more flexible and maintainable code.