1.
Which Design Pattern has this Intent?
-
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Correct Answer
C. Prototype
Explanation
The correct answer is Prototype. The Prototype design pattern is used to specify the kinds of objects to create using a prototypical instance and then create new objects by copying this prototype. It allows for creating new objects without specifying their exact class, instead using a prototype object as a template. This pattern is useful when creating multiple instances of similar objects and can improve performance by avoiding the need to create new objects from scratch each time.
2.
Which Design Pattern has this Intent?
- Provide a surrogate or placeholder for another object to control access to it.
Correct Answer
A. Proxy
Explanation
The Proxy design pattern provides a surrogate or placeholder for another object to control access to it. This means that instead of directly accessing the actual object, the client interacts with the proxy object, which then forwards the requests to the real object. The proxy can add additional functionality such as caching, logging, or security checks without the client being aware of it.
3.
Which Design Pattern has this Intent?
-
Ensure a class only has one instance and provide a global point of access to it.
Correct Answer
B. Singleton
Explanation
The correct answer is Singleton. The Singleton design pattern ensures that a class only has one instance and provides a global point of access to it. This pattern is useful when there should be only one instance of a class throughout the system, such as a database connection or a logger. The Singleton pattern restricts the instantiation of a class to a single object and provides a way to access that object globally.
4.
Which Design Pattern has this Intent?
-
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Correct Answer
D. State
Explanation
The correct answer is State. The State design pattern allows an object to change its behavior when its internal state changes. This pattern is useful when an object needs to change its behavior dynamically based on its internal state, and the object appears to change its class to the outside world.
5.
Which Design Pattern has this Intent?
-
Define a family of algorithms, encapsulate each one, and make them interchangeable. _____ lets the algorithm vary independently from clients that use it.
Correct Answer
B. Strategy
Explanation
The correct answer is Strategy. The Strategy design pattern allows for the definition of a family of algorithms, encapsulating each one and making them interchangeable. This pattern enables the algorithm to vary independently from the clients that use it, providing flexibility and allowing for easy switching between different algorithms based on the specific needs of the client.
6.
Which Design Pattern has this Intent?
- Define the skeleton of an algorithm in operation, deferring some steps to subclasses. _____ lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Correct Answer
C. Template Method
Explanation
The correct answer is Template Method. The Template Method design pattern defines the skeleton of an algorithm in an abstract class, with certain steps left to be implemented by subclasses. This allows subclasses to redefine or customize specific steps of the algorithm without altering the overall structure of the algorithm.
7.
Which Design Pattern has this Intent?
-
Represent an operation to be performed on the elements of an object structure. ____ lets you define a new
operation without changing the classes of the elements on which it operates.
Correct Answer
C. Visitor
Explanation
The Visitor design pattern has the intent of representing an operation to be performed on the elements of an object structure. It allows you to define a new operation without changing the classes of the elements on which it operates. This pattern is useful when you have a complex object structure and want to perform different operations on its elements without modifying their classes. The Visitor pattern separates the algorithm for performing the operation from the objects being operated on, promoting flexibility and extensibility.
8.
Which Design Pattern has this Intent?
-
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Correct Answer
C. Abstract Factory
Explanation
The correct answer is Abstract Factory because it is a design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. In other words, the Abstract Factory pattern allows the creation of objects that are related or dependent on each other, but the specific classes of those objects are not known or specified. This pattern promotes loose coupling and encapsulation by abstracting the process of object creation.
9.
Which Design Pattern has this Intent?
-
Convert the interface of a class into another interface clients expect.
Correct Answer
B. Adapter
Explanation
The Adapter design pattern is used to convert the interface of a class into another interface that clients expect. It allows classes with incompatible interfaces to work together by creating a bridge between them. The Adapter pattern is useful when we want to reuse existing classes without modifying their code, or when we want to create a new class that interacts with existing classes using a different interface.
10.
Which Design Pattern has this Intent?
-
Decouple an abstraction from its implementation so that the two can vary independently.
Correct Answer
A. Bridge
Explanation
The Bridge design pattern is the correct answer because it is specifically designed to decouple an abstraction from its implementation. It allows the two to vary independently, meaning that changes in one do not affect the other. This pattern is useful when there is a need to separate the abstraction and implementation hierarchies and have them evolve independently. The Bridge pattern promotes flexibility, extensibility, and maintainability in a system.
11.
Which Design Pattern has this Intent?
-
Separate the construction of a complex object from its representation so that the same construction process can
create different representations.
Correct Answer
D. Builder
Explanation
The Builder design pattern is the correct answer because it separates the construction of a complex object from its representation. This allows the same construction process to create different representations of the object. The Builder pattern is useful when there are multiple steps involved in creating an object and when different variations of the object need to be constructed.
12.
Which Design Pattern has this Intent?
-
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
Correct Answer
B. Chain of Responsibility
Explanation
The Chain of Responsibility design pattern is the correct answer because it aims to avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. In this pattern, a chain of objects is created, and each object in the chain has the ability to either handle the request or pass it on to the next object in the chain. This allows for a flexible and decoupled way of handling requests, as any object in the chain can potentially handle the request without the sender needing to know which object will handle it.
13.
Which Design Pattern has this Intent?
-
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Correct Answer
B. Command
Explanation
The Command design pattern encapsulates a request as an object, allowing clients to be parameterized with different requests. This pattern also supports queuing or logging of requests, and enables undoable operations. In the Command pattern, a command object is created that encapsulates a specific request and its parameters. This allows the request to be passed as a parameter to a method, enabling the method to execute the request at a later time. This pattern is useful in scenarios where the client and the object executing the request need to be decoupled, or when multiple requests need to be queued or logged.
14.
Which Design Pattern has this Intent?
-
Compose objects into tree structures to represent part-whole hierarchies. Clients treat collections of objects and individual objects uniformly.
Correct Answer
A. Composite
Explanation
The correct answer is Composite. The Composite design pattern is used to compose objects into tree structures to represent part-whole hierarchies. It allows clients to treat collections of objects and individual objects uniformly. This pattern is useful when dealing with hierarchical structures where objects can be composed of other objects in a recursive manner.
15.
Which Design Pattern has this Intent?
-
Attach additional responsibilities to an object dynamically. _____ provides a flexible alternative to subclassing
for extending functionality.
Correct Answer
D. Decorator
Explanation
The Decorator design pattern allows additional responsibilities to be attached to an object dynamically, providing a flexible alternative to subclassing for extending functionality. This pattern allows behavior to be added to an object without affecting its structure. It involves creating a decorator class that wraps the original object and adds new behaviors or responsibilities to it at runtime. This allows for easy customization and extension of the object's functionality without the need to create multiple subclasses.
16.
Which Design Pattern has this Intent?
-
Provide a unified interface to a set of interfaces in a subsystem. _____ defines a higher-level interface that makes the subsystem easier to use.
Correct Answer
C. Facade
Explanation
The correct answer is Facade. The Facade design pattern provides a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use. The Facade pattern simplifies the complexity of a system by providing a single interface that clients can interact with, hiding the inner workings and complexities of the subsystem.
17.
Which Design Pattern has this Intent?
-
Define an interface for creating an object, but let subclasses decide which class to instantiate. ____ lets a class defer instantiation to subclasses.
Correct Answer
C. Factory Method
Explanation
The Factory Method design pattern is the correct answer for this question. This pattern allows a class to defer the instantiation of an object to its subclasses. It defines an interface for creating objects, but the actual instantiation is left to the subclasses, which can decide which class to instantiate. This promotes flexibility and extensibility in the code, as different subclasses can create different types of objects based on their specific requirements.
18.
Which Design Pattern has this Intent?
-
Use sharing to support large numbers of fine-grained objects efficiently.
Correct Answer
B. Flyweight
Explanation
The Flyweight design pattern is used to support large numbers of fine-grained objects efficiently by sharing them. It achieves this by separating the intrinsic and extrinsic state of objects. The intrinsic state is shared among multiple objects, while the extrinsic state is unique to each object. By sharing the common intrinsic state, the Flyweight pattern reduces memory usage and improves performance.
19.
Which Design Pattern has this Intent?
-
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
Correct Answer
B. Interpreter
Explanation
The correct answer is Interpreter. The intent of the Interpreter design pattern is to define a language and provide a way to interpret sentences in that language. In this scenario, the design pattern would involve defining a representation for the grammar of a language and creating an interpreter that can interpret sentences in that language using the defined representation.
20.
Which Design Pattern has this Intent?
-
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Correct Answer
C. Iterator
Explanation
The correct answer is Iterator. The Iterator design pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It allows clients to iterate over elements of a collection without knowing the internal structure of the collection. This pattern is useful when there is a need to traverse a collection of objects in a specific order, without exposing the details of how the collection is implemented. The Iterator pattern decouples the client code from the implementation of the collection, making it more flexible and maintainable.
21.
Which Design Pattern has this Intent?
-
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 their interaction independently.
Correct Answer
A. Mediator
Explanation
The Mediator design pattern is the correct answer for this question. 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, and it lets you vary their interaction independently.
22.
Which Design Pattern has this Intent?
-
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
Correct Answer
D. Memento
Explanation
The Memento design pattern is used to capture and externalize an object's internal state without violating encapsulation. It allows the object to be restored to this state later. This pattern is useful when we need to save and restore the state of an object, such as in undo/redo functionality or checkpoint systems.
23.
Which Design Pattern has this Intent?
-
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are
notified and updated automatically.
Correct Answer
B. Observer
Explanation
The Observer design pattern is used to establish a one-to-many dependency between objects. It allows multiple observers (dependents) to be notified and updated automatically when the state of the observed object changes. This pattern is useful when there is a need for loose coupling between objects, where the observed object doesn't need to know about its observers, and the observers can be easily added or removed without affecting the observed object.