責任鏈模式git
1.3 純與不純github
public abstract class Handler { protected Handler nextHandler = null; public abstract void handle(); public Handler getNextHandler() { return nextHandler; } public void setNextHandler(Handler nextHandler) { this.nextHandler = nextHandler; } }
public class HandlerA extends Handler{ @Override public void handle() { if(nextHandler == null){ System.out.println("HandlerA handle ..."); }else{ nextHandler.handle(); } } } public class HandlerB extends Handler{ @Override public void handle() { if(nextHandler == null){ System.out.println("HandlerB handle ..."); }else{ nextHandler.handle(); } } }
public static void main(String[] args) { Handler handlerA = new HandlerA(); Handler handlerB = new HandlerB(); handlerA.setNextHandler(handlerB); handlerA.handle(); }
HandlerB handle ...
https://github.com/Seasons20/DisignPattern.git
ENDide