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.
What is Java Generics?
Java Generics, introduced in Java 5.0, add stability by enabling compile-time type checking and reducing runtime errors caused by incorrect type casting
Why use Java Generics?
Facilitates generic programming, allowing algorithms to be written for unspecified types
Enables type-safe code reuse
Detects potential runtime errors at compile time
What is a generic container?
A container class with a type variable (e.g., T) allowing the use of specific types without needing explicit casting
What are the naming conventions for type parameters (E,K,N,T,V,SUV)?
Common type parameter names:
E – Element
K – Key
N – Number
T – Type
V – Value
S, U, V – Secondary types
What are the types of generics in Java?
Generic Types
Generic Methods
Generic Constructors
What is type inference in methods?
Type can be inferred without explicit specification if the compiler can deduce it from the context (e.g., using diamonds < > introduced in Java 7)
What are bounded types?
Allows restricting type parameters:
Upper bound: <T></T>
Lower bound: <T></T>
What is the role of wildcards (?)?
Represent unknown types, enabling flexibility:
Upper bound: <? extends Type>
Lower bound: <? super Type>
What is type erasure?
Type information is removed during compilation to ensure backward compatibility
Generic types exist only at compile time and are replaced by raw types at runtime
Why should legacy and generic types not be mixed?
Mixing can lead to warnings about unchecked or unsafe operations and negate the safety benefits of generics
What is heap pollution?
Occurs when a variable of a parameterized type refers to an instance of an incompatible type
Often results in unchecked warnings
What are the benefits of using Java Generics?
Stronger compile-time type checks
Elimination of explicit casting
Simplified and type-safe code
What is a Collection?
An object that groups multiple elements into a single unit
eg.
A poker hand: a collection of cards
A mail folder: a collection of letters
A telephone directory: A mappng of names to phone numbers
What is the difference between a Collection vs Map?
Collection - group of objects known as elements
public interface Collection<E></E>
Map - group of objects where each object maps to key values
public interface Map<K, V>