Design Patterns Flashcards

1
Q

What do we need for a strategy pattern?

A
  1. Interface for our functions/behaviors
    — Interface are just methods/rules the other classes must have
  2. New class implements the behavior and decides what the new behavior should be
  3. Create the context class that has the set behaviors, this will be the constructor. The class should take in parameters that define which behaviors should have
  4. Create different objects with X behavior
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the strategy pattern?

A

A behavioral design pattern that allows a class to choose its behavior dynamically by delegating it to an interchangeable strategy object.

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

What is the main advantage of the Strategy Pattern?

A

It allows you to change an algorithm’s behavior at runtime without modifying the context class, making the system more flexible and maintainable.

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

What is the role of a strategy interface in the Strategy Pattern?

A

The strategy interface defines a common contract (set of methods) that all concrete strategies must implement. It allows the context class to use different strategies interchangeably.

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

What are some real-world applications of the Strategy Pattern?

A
  1. Sorting algorithms (Bubble Sort, Quick Sort, Merge Sort)
  2. Character movement in games (different walking/running behaviors)
  3. Payment processing systems (Credit card, PayPal, Bitcoin)
  4. File compression algorithms (ZIP, RAR, TAR)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a context class in the Strategy Pattern?

A

A class that delegates certain behavior to a strategy object. It holds a reference to a strategy and calls its method.

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

How do you implement different strategies in Java?

A

By creating concrete classes that implement the strategy interface.

class FlyWithWings implements FlyBehavior {
public void fly() {
System.out.println(“I’m flying with wings!”);
}
}

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

Give an example of a strategy interface in Java.

A

interface FlyBehavior {
void fly();
}

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

What is the Observer Pattern?

A

The Observer Pattern is a behavioral design pattern where an object (subject) maintains a list of dependents (observers) and automatically notifies them of any state changes.

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

Why use the Observer Pattern?

A

It helps decouple objects, so multiple parts of a program can listen for changes without modifying the subject directly. Useful for event-driven programming.

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

How does the Observer Pattern work?

A
  1. A subject (observable) maintains a list of observers.
  2. When the subject changes, it notifies all observers.
  3. Observers update themselves automatically.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are some real-world examples of the Observer Pattern?

A
  1. News subscriptions (Subscribers receive updates when new articles are published).
  2. Stock market tracking (Investors get notified of stock price changes).
  3. GUI event listeners (Buttons notify handlers when clicked).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the Singleton Pattern?

A

A design pattern that ensures a class has only one instance and provides a global access point to it.

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

Why use the Singleton Pattern?

A
  1. Prevents multiple instances of an object.
  2. Saves memory by reusing the same instance.
  3. Ensures controlled access to shared resources (like databases or logs).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are some real-world examples of the Singleton Pattern?

A
  1. Database connections (Only one instance should manage queries).
  2. Logging system (One shared instance to record logs).
  3. Game settings (One instance storing player preferences).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are potential problems with the Singleton Pattern?

A
  1. Thread safety issues (If not handled correctly, multiple instances may be created in multi-threaded programs).
  2. Difficult to test (Singletons can be hard to mock in unit tests).
  3. Breaks Single Responsibility Principle (Handles both instance control and logic).