Chain of Responsibility pattern
Created on: Sep 25, 2024
The Chain of Responsibility pattern is a behavioral design pattern that allows you to pass a request along a chain of handlers, each 1 of which can potentially handle the request. This pattern is useful when you have multiple objects that can handle a request, and you want to avoid coupling the sender of the request to the receiver.
public class Currency { private int amount; public Currency(int amount) { this.amount = amount; } public int getAmount() { return amount; } } public interface DispenseChain { void setNextchain(DispenseChain nextchain); void dispense(Currency currency); }
public class Dollar10Dispenser implements DispenseChain{ private DispenseChain dispenseChain; @Override public void setNextchain(DispenseChain nextchain) { this.dispenseChain = nextchain; } @Override public void dispense(Currency currency) { if(currency.getAmount()>=10){ int num = currency.getAmount()/10; int remender = currency.getAmount()%10; System.out.println("Dispensing "+num+" $10 notes"); if(remender!=0) dispenseChain.dispense(new Currency(remender)); }else{ dispenseChain.dispense(currency); } } } public class Dollar20Dispenser implements DispenseChain{ private DispenseChain dispenseChain; @Override public void setNextchain(DispenseChain nextchain) { this.dispenseChain = nextchain; } @Override public void dispense(Currency currency) { if(currency.getAmount()>=20){ int notesCount = currency.getAmount()/20; int leftAmount = currency.getAmount() % 20; System.out.println("Dispense "+notesCount+" of $20"); if(leftAmount!=0) dispenseChain.dispense(new Currency(leftAmount)); }else{ dispenseChain.dispense(currency); } } } public class Dollar50Dispenser implements DispenseChain { private DispenseChain dispenseChain; @Override public void setNextchain(DispenseChain nextchain) { this.dispenseChain = nextchain; } @Override public void dispense(Currency currency) { if (currency.getAmount() >= 50) { int notesCount = currency.getAmount() / 50; int leftAmount = currency.getAmount() % 50; System.out.println("Dispense "+notesCount+" of $50"); if(leftAmount!=0) dispenseChain.dispense(new Currency(leftAmount)); }else{ dispenseChain.dispense(currency); } } }
public class AtmDispenseApplication{ public static void main(String[] args) { DispenseChain dispense50Dollar = new Dollar50Dispenser(); DispenseChain dispense20Dollar = new Dollar20Dispenser(); DispenseChain dispense10Dollar = new Dollar10Dispenser(); dispense50Dollar.setNextchain(dispense20Dollar); dispense20Dollar.setNextchain(dispense10Dollar); dispense10Dollar.setNextchain(null); dispense50Dollar.dispense(new Currency(580)); } }
When to use
- When multiple objects needs to handle the request.
- Request filtering: For example filtering request based on user authentication
- Request validation: For example, you could use it to validate input data or check for security vulnerabilities.
- Asynchronous processing: You can use the Chain of Responsibility pattern to implement asynchronous processing. For example, you could use it to queue requests for processing by a background worker.
