Which of the following scenarios best exemplifies the use of the Strategy Pattern?
Explanation:
The Strategy Pattern shines when you have a family of related algorithms (validation rules), and the choice of which algorithm to apply can change dynamically based on the context (input field type, user role, etc.).
Which of these is a common real-world analogy used to explain the Decorator Pattern?
Explanation:
Just like each topping adds a specific flavor or texture without changing the base product, decorators add functionalities without modifying the core object.
In the Adapter Pattern, what is the 'Adaptee'?
Explanation:
The 'Adaptee' is the existing class with an interface that needs modification to work with the client. The Adapter Pattern wraps around the Adaptee to provide the desired interface.
Which of these is NOT a benefit of using design patterns?
Explanation:
Design patterns aim to reduce code duplication by providing reusable solutions, not increase it. They promote maintainability, readability, and reusability.
In the Observer pattern, what does the 'subject' object do?
Explanation:
The 'subject' is responsible for keeping track of its observers and providing methods for observers to register and unregister themselves. It also notifies the observers when its state changes.
Which of these scenarios would be a suitable use case for the Decorator Pattern?
Explanation:
You can create a base decorator for logging and then specific decorators for different log levels or targets, dynamically adding them to components as needed.
Which of the following is NOT a typical category of design patterns?
Explanation:
The three primary categories of design patterns are Creational, Structural, and Behavioral. Functional programming is a paradigm, not a design pattern category.
Can you have multiple Facade classes for a single complex subsystem?
Explanation:
You can have multiple Facade classes for a subsystem, each providing a different level of abstraction or focusing on a specific subset of functionalities within that subsystem.
How does the Command Pattern support the implementation of an 'undo' functionality?
Explanation:
A well-designed Command object can include not only the execution logic but also logic for how to undo its effect. This allows the application to revert changes step-by-step.
Which of these is NOT a core component of the Strategy Pattern?
Explanation:
While a factory can be used to create Strategy objects, it's not a fundamental part of the Strategy Pattern itself. The core components are Strategy (interface), ConcreteStrategy (implementation), and Context (using the strategy).
How does the Command Pattern achieve decoupling?
Explanation:
The Command object acts as a link. The Invoker knows to execute the command, but not the specifics of what it does. The Receiver handles the action, but isn't directly tied to who made the request.
What is the primary intent of the Factory Method pattern in software development?
Explanation:
The core idea behind the Factory Method pattern is to define an interface for creating objects, but let subclasses decide which classes to instantiate. This promotes the Open/Closed Principle, allowing for extensibility without modifying existing code.
How does the Strategy Pattern promote the Open/Closed Principle?
Explanation:
The Open/Closed Principle advocates for systems that are open for extension but closed for modification. With the Strategy Pattern, you can introduce new algorithms (strategies) without altering the core Context class, adhering to this principle.
How does the Decorator Pattern differ from traditional inheritance for extending functionality?
Explanation:
Unlike inheritance, which is static and defined at compile time, Decorator allows you to mix and match decorations dynamically during execution.
What is a potential drawback of using the Factory Method pattern?
Explanation:
While powerful, Factory Method can make your codebase more intricate, particularly when dealing with a large number of concrete product types. Each new product often requires a corresponding concrete factory subclass.