Week 8 Flashcards

1
Q

What is the Decorator Pattern?

A

The Decorator Pattern attaches additional behavioral responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What problem does the Decorator Pattern solve in the coffee shop example?

A

It avoids class explosion caused by subclassing every possible combination of coffee and condiments by dynamically adding condiments as decorators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the key principles behind the Decorator Pattern?

A
  1. Code should be open for extension but closed for modification (Open-Closed Principle)
  2. Favor object composition over class inheritance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the components of the Decorator Pattern?

A
  1. Component: Defines an interface for objects.
  2. ConcreteComponent: Implements the Component interface.
  3. Decorator: Wraps a Component and modifies its behavior.
  4. ConcreteDecorator: Adds new responsibilities to the Component.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are condiments added in the Decorator Pattern?

A

Condiments are treated as decorators that wrap the base beverage. Each decorator adds its own cost and description

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Command Pattern?

A

The Command Pattern encapsulates a request as an object, allowing it to parameterize objects with requests, queue requests, or support undoable operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the main roles in the Command Pattern?

A
  1. Invoker: Holds and triggers the command
  2. Command: Declares an interface for executing actions
  3. ConcreteCommand: Binds a receiver to an action
  4. Receiver: Performs the action
  5. Client: Configures the ConcreteCommand and assigns the Receiver
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does the Command Pattern apply to a remote control example?

A

Each button on the remote control acts as an Invoker, sending requests encapsulated in ConcreteCommands to corresponding Receivers (e.g., lights, garage doors).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the advantages of the Command Pattern?

A

1.Decouples sender and receiver.

  1. Supports queuing and logging requests.
  2. Enables undo functionality.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are examples of the patterns covered in the lecture?

A
  1. Decorator Pattern: Dynamically extends functionality
  2. Command Pattern: Encapsulates requests as objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the key OO design principles supporting these patterns?

A
  1. Encapsulate what varies.
  2. Program to an interface, not an implementation
  3. Favor object composition over inheritance
  4. Classes should be open for extension but closed for modification
  5. Depend upon abstractions, not concrete classes (Dependency Inversion Principle)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the “Open-Closed Principle”?

A

A design principle stating that classes should be open for extension but closed for modification

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is dynamic runtime decoration in the Decorator Pattern?

A

Objects can be decorated at any time, allowing behavior to be dynamically added during program execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an example of dynamic decoration in Java?

A

Wrapping a DarkRoast object with Mocha and Whip decorators to compute the total cost dynamically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are some limitations of the Decorator Pattern?

A
  1. Introduces new levels of abstraction.
  2. Can make the object composition more complex and harder to manage.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Java Generics?

A

Java Generics, introduced in Java 5.0, add stability by enabling compile-time type checking and reducing runtime errors caused by incorrect type casting

17
Q

Why use Java Generics?

A

Facilitates generic programming, allowing algorithms to be written for unspecified types

Enables type-safe code reuse

Detects potential runtime errors at compile time

18
Q

What is a generic container?

A

A container class with a type variable (e.g., T) allowing the use of specific types without needing explicit casting

19
Q

What are the naming conventions for type parameters?

A

Common type parameter names:

E – Element
K – Key
N – Number
T – Type
V – Value
S, U, V – Secondary types

20
Q

What are the types of generics in Java?

A

Generic Types

Generic Methods

Generic Constructors

21
Q

What is type inference in methods?

A

Type can be inferred without explicit specification if the compiler can deduce it from the context (e.g., using diamonds < > introduced in Java 7)

22
Q

What are bounded types?

A

Allows restricting type parameters:

Upper bound: <T></T>

Lower bound: <T></T>

23
Q

What is the role of wildcards (?)?

A

Represent unknown types, enabling flexibility:

Upper bound: <? extends Type>

Lower bound: <? super Type>

24
Q

What is type erasure?

A

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

25
Q

Why should legacy and generic types not be mixed?

A

Mixing can lead to warnings about unchecked or unsafe operations and negate the safety benefits of generics

26
Q

What is heap pollution?

A

Occurs when a variable of a parameterized type refers to an instance of an incompatible type

Often results in unchecked warnings

27
Q

What are the benefits of using Java Generics?

A

Stronger compile-time type checks

Elimination of explicit casting

Simplified and type-safe code