Object Interaction / Responsibility
Chain of responsibility
The Chain of Responsibility pattern is a behavioral design pattern where a request is passed through a chain of handlers. Each handler decides either to process the request or to pass it to the next handler in the chain.
// Define a handler interface
interface Handler {
void handleRequest(Request request);
void setNextHandler(Handler nextHandler);
}
// Concrete handler 1
class ConcreteHandler1 implements Handler {
private Handler nextHandler;
@Override
public void handleRequest(Request request) {
if (request.getLevel() <= 10) {
System.out.println("Request handled by ConcreteHandler1");
} else if (nextHandler != null) {
nextHandler.handleRequest(request);
} else {
System.out.println("Request cannot be handled");
}
}
@Override
public void setNextHandler(Handler nextHandler) {
this.nextHandler = nextHandler;
}
}
// Concrete handler 2
class ConcreteHandler2 implements Handler {
private Handler nextHandler;
@Override
public void handleRequest(Request request) {
if (request.getLevel() > 10 && request.getLevel() <= 20) {
System.out.println("Request handled by ConcreteHandler2");
} else if (nextHandler != null) {
nextHandler.handleRequest(request);
} else {
System.out.println("Request cannot be handled");
}
}
@Override
public void setNextHandler(Handler nextHandler) {
this.nextHandler = nextHandler;
}
}
// Request class
class Request {
private int level;
public Request(int level) {
this.level = level;
}
public int getLevel() {
return level;
}
}
public class Main {
public static void main(String[] args) {
// Create handlers
Handler handler1 = new ConcreteHandler1();
Handler handler2 = new ConcreteHandler2();
// Set the chain of responsibility
handler1.setNextHandler(handler2);
// Send requests to the chain
handler1.handleRequest(new Request(5));
handler1.handleRequest(new Request(15));
handler1.handleRequest(new Request(25));
}
}
Command
The Command pattern is a behavioral design pattern that encapsulates a request as an object, thereby allowing you to parameterize clients with queues, requests, and operations.
Iterator
The Iterator pattern is a behavioral design pattern that provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.
Mediator
The Mediator pattern is a behavioral design pattern that promotes loose coupling by centralizing communication between different objects. Instead of objects communicating directly with each other, they communicate through a mediator object.
Memento
The Memento pattern is a behavioral design pattern that allows an object to capture its internal state without exposing its implementation details and then restore that state later without violating encapsulation. This pattern is useful when you need to implement undo/redo functionality or maintain checkpoints in an application.
Observer
The Observer pattern is a behavioral design pattern where an object, known as the subject, maintains a list of its dependents, called observers, and notifies them of any state changes, usually by calling one of their methods.
Command
The State pattern is a behavioral design pattern that allows an object to alter its behavior when its internal state changes. This pattern is useful when an object's behavior depends on its state, and it needs to change its behavior dynamically based on changes to its internal state.
Strategy
The Strategy pattern is a behavioral design pattern that enables a client to choose from a family of algorithms at runtime. It defines a family of algorithms, encapsulates each algorithm, and makes them interchangeable. This pattern allows the algorithm to vary independently from the clients that use it.
Last updated