What do we need for a strategy pattern?
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?
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?
What are some real-world examples of the Observer Pattern?
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?
What are some real-world examples of the Singleton Pattern?
What are potential problems with the Singleton Pattern?