Design Patterns Flashcards
What do we need for a strategy pattern?
- Interface for our functions/behaviors
— Interface are just methods/rules the other classes must have - New class implements the behavior and decides what the new behavior should be
- 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
- Create different objects with X behavior
What is the strategy pattern?
A behavioral design pattern that allows a class to choose its behavior dynamically by delegating it to an interchangeable strategy object.
What is the main advantage of the Strategy Pattern?
It allows you to change an algorithm’s behavior at runtime without modifying the context class, making the system more flexible and maintainable.
What is the role of a strategy interface in the Strategy Pattern?
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.
What are some real-world applications of the Strategy Pattern?
- Sorting algorithms (Bubble Sort, Quick Sort, Merge Sort)
- Character movement in games (different walking/running behaviors)
- Payment processing systems (Credit card, PayPal, Bitcoin)
- File compression algorithms (ZIP, RAR, TAR)
What is a context class in the Strategy Pattern?
A class that delegates certain behavior to a strategy object. It holds a reference to a strategy and calls its method.
How do you implement different strategies in Java?
By creating concrete classes that implement the strategy interface.
class FlyWithWings implements FlyBehavior {
public void fly() {
System.out.println(“I’m flying with wings!”);
}
}
Give an example of a strategy interface in Java.
interface FlyBehavior {
void fly();
}
What is the Observer Pattern?
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.
Why use the Observer Pattern?
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 does the Observer Pattern work?
- A subject (observable) maintains a list of observers.
- When the subject changes, it notifies all observers.
- Observers update themselves automatically.
What are some real-world examples of the Observer Pattern?
- News subscriptions (Subscribers receive updates when new articles are published).
- Stock market tracking (Investors get notified of stock price changes).
- GUI event listeners (Buttons notify handlers when clicked).
What is the Singleton Pattern?
A design pattern that ensures a class has only one instance and provides a global access point to it.
Why use the Singleton Pattern?
- Prevents multiple instances of an object.
- Saves memory by reusing the same instance.
- Ensures controlled access to shared resources (like databases or logs).
What are some real-world examples of the Singleton Pattern?
- Database connections (Only one instance should manage queries).
- Logging system (One shared instance to record logs).
- Game settings (One instance storing player preferences).
What are potential problems with the Singleton Pattern?
- Thread safety issues (If not handled correctly, multiple instances may be created in multi-threaded programs).
- Difficult to test (Singletons can be hard to mock in unit tests).
- Breaks Single Responsibility Principle (Handles both instance control and logic).