🔹 Behavioral Design Patterns in OOP
**Behavioral Design Patterns** focus on how objects **interact and communicate** with each other. These patterns improve flexibility by defining **how responsibilities are distributed among objects**.
📌 Why Use Behavioral Patterns?
Behavioral patterns help in managing **object interactions** efficiently.
- ✅ **Reduces Code Complexity** – Defines clear rules for object communication.
- ✅ **Encapsulation of Behavior** – Keeps business logic modular.
- ✅ **Enhances Maintainability** – Easy to modify interactions between objects.
- ✅ **Promotes Code Reusability** – Behavior can be reused in different parts of the program.
📌 Common Behavioral Patterns
🔹 1. Strategy Pattern
Defines a **family of algorithms** and selects one at runtime.
interface PaymentStrategy {
void pay(int amount);
}
class CreditCardPayment implements PaymentStrategy {
public void pay(int amount) {
System.out.println("Paid $" + amount + " using Credit Card.");
}
}
class PayPalPayment implements PaymentStrategy {
public void pay(int amount) {
System.out.println("Paid $" + amount + " using PayPal.");
}
}
class PaymentProcessor {
private PaymentStrategy strategy;
public PaymentProcessor(PaymentStrategy strategy) {
this.strategy = strategy;
}
void executePayment(int amount) {
strategy.pay(amount);
}
}
// Usage
public class Main {
public static void main(String[] args) {
PaymentProcessor payment = new PaymentProcessor(new PayPalPayment());
payment.executePayment(100);
}
}
🔹 2. Observer Pattern
Allows multiple objects (**observers**) to react when a subject (**observable**) changes.
import java.util.ArrayList;
import java.util.List;
interface Observer {
void update(String message);
}
class Subscriber implements Observer {
private String name;
Subscriber(String name) {
this.name = name;
}
public void update(String message) {
System.out.println(name + " received: " + message);
}
}
class YouTubeChannel {
private List subscribers = new ArrayList<>();
void subscribe(Observer o) {
subscribers.add(o);
}
void notifySubscribers(String message) {
for (Observer o : subscribers) {
o.update(message);
}
}
}
// Usage
public class Main {
public static void main(String[] args) {
YouTubeChannel channel = new YouTubeChannel();
Observer user1 = new Subscriber("Alice");
Observer user2 = new Subscriber("Bob");
channel.subscribe(user1);
channel.subscribe(user2);
channel.notifySubscribers("New video uploaded!");
}
}
🔹 3. Command Pattern
Encapsulates a request as an object to **support undo/redo actions**.
interface Command {
void execute();
}
class LightOnCommand implements Command {
public void execute() {
System.out.println("Light is turned ON");
}
}
class RemoteControl {
private Command command;
RemoteControl(Command command) {
this.command = command;
}
void pressButton() {
command.execute();
}
}
// Usage
public class Main {
public static void main(String[] args) {
Command lightOn = new LightOnCommand();
RemoteControl remote = new RemoteControl(lightOn);
remote.pressButton();
}
}
🎯 Summary
Behavioral design patterns **define object communication** while keeping business logic independent. Common patterns include **Strategy, Observer, and Command**.