Week 8 Flashcards
What is the Decorator Pattern?
The Decorator Pattern attaches additional behavioral responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality
What problem does the Decorator Pattern solve in the coffee shop example?
It avoids class explosion caused by subclassing every possible combination of coffee and condiments by dynamically adding condiments as decorators
What are the key principles behind the Decorator Pattern?
- Code should be open for extension but closed for modification (Open-Closed Principle)
- Favor object composition over class inheritance
What are the components of the Decorator Pattern?
- Component: Defines an interface for objects.
- ConcreteComponent: Implements the Component interface.
- Decorator: Wraps a Component and modifies its behavior.
- ConcreteDecorator: Adds new responsibilities to the Component.
How are condiments added in the Decorator Pattern?
Condiments are treated as decorators that wrap the base beverage. Each decorator adds its own cost and description
What is the Command Pattern?
The Command Pattern encapsulates a request as an object, allowing it to parameterize objects with requests, queue requests, or support undoable operations
What are the main roles in the Command Pattern?
- Invoker: Holds and triggers the command
- Command: Declares an interface for executing actions
- ConcreteCommand: Binds a receiver to an action
- Receiver: Performs the action
- Client: Configures the ConcreteCommand and assigns the Receiver
How does the Command Pattern apply to a remote control example?
Each button on the remote control acts as an Invoker, sending requests encapsulated in ConcreteCommands to corresponding Receivers (e.g., lights, garage doors).
What are the advantages of the Command Pattern?
1.Decouples sender and receiver.
- Supports queuing and logging requests.
- Enables undo functionality.
What are examples of the patterns covered in the lecture?
- Decorator Pattern: Dynamically extends functionality
- Command Pattern: Encapsulates requests as objects
What are the key OO design principles supporting these patterns?
- Encapsulate what varies.
- Program to an interface, not an implementation
- Favor object composition over inheritance
- Classes should be open for extension but closed for modification
- Depend upon abstractions, not concrete classes (Dependency Inversion Principle)
What is the “Open-Closed Principle”?
A design principle stating that classes should be open for extension but closed for modification
What is dynamic runtime decoration in the Decorator Pattern?
Objects can be decorated at any time, allowing behavior to be dynamically added during program execution
What is an example of dynamic decoration in Java?
Wrapping a DarkRoast object with Mocha and Whip decorators to compute the total cost dynamically.
What are some limitations of the Decorator Pattern?
- Introduces new levels of abstraction.
- Can make the object composition more complex and harder to manage.