设计模式-行为型模式
Posted strong-fe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式-行为型模式相关的知识,希望对你有一定的参考价值。
一、责任链模式
用于避免请求发送者与多个请求处理者耦合在一起,让所有请求的处理者持有下一个对象的引用,从而将请求串联成一条链,在有请求发生时,可将请求沿着这条链传递,直到遇到该对象的处理器。
通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。
以用户发送Web请求,经过用户鉴权、业务调用、结果反馈流程为例:
(1)定义Handler接口:
public interface Handler { void operator(); }
(2)定义AbstractHandler类:
public abstract class AbstractHandler { private Handler handler; public Handler getHandler() { return handler; } public void setHandler(Handler handler) { this.handler = handler; } }
(3)定义用户授权类AuthHandler、业务处理类BusinessHandler、结果反馈类ResponseHandler
public class AuthHandler extends AbstractHandler implements Handler { private final static Log logger = LogFactory.getLog(AuthHandler.class); private String name; public AuthHandler(String name){ this.name = name; } @Override public void operator() { logger.info("user auth"); if(getHandler()!=null){ //执行责任链下一流程 getHandler().operator(); } } } public class BusinessHandler extends AbstractHandler implements Handler { private final static Log logger = LogFactory.getLog(BusinessHandler.class); private String name; public BusinessHandler(String name){ this.name = name; } @Override public void operator() { logger.info("Business info handler"); if(getHandler()!=null){ //执行责任链下一流程 getHandler().operator(); } } } public class ResponseHandler extends AbstractHandler implements Handler { private final static Log logger = LogFactory.getLog(ResponseHandler.class); private String name; public ResponseHandler(String name){ this.name = name; } @Override public void operator() { logger.info("message response"); if(getHandler()!=null){ //执行责任链下一流程 getHandler().operator(); } } }
(4)使用责任链模型:
public class Main { public static void main(String[] args) { AuthHandler authHandler = new AuthHandler("auth"); BusinessHandler businessHandler = new BusinessHandler("business"); ResponseHandler responseHandler = new ResponseHandler("response"); authHandler.setHandler(businessHandler); businessHandler.setHandler(responseHandler); authHandler.operator(); } }
结果:
十二月 30, 2019 10:06:15 下午 com.jzq.sign.Responsibility.AuthHandler operator 信息: user auth 十二月 30, 2019 10:06:15 下午 com.jzq.sign.Responsibility.BusinessHandler operator 信息: Business info handler 十二月 30, 2019 10:06:15 下午 com.jzq.sign.Responsibility.ResponseHandler operator 信息: message response
二、命令模式
请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。
(1)创建一个Command接口:
public interface Command { void execute(); }
(2)创建请求类Stock:
public class Stock { private String name = "ABC"; private int quantity = 10; public void buy(){ System.out.println("Stock [ Name: "+name+",Quantity: " + quantity +" ] bought"); } public void sell(){ System.out.println("Stock [ Name: "+name+",Quantity: " + quantity +" ] sold"); } }
(3)创建实现了Command 接口的实体类BuyStock和SellStock
public class BuyStock implements Command { private Stock abcStock; public BuyStock(Stock abcStock){ this.abcStock = abcStock; } @Override public void execute() { abcStock.buy(); } } public class SellStock implements Command { private Stock abcStock; public SellStock(Stock abcStock){ this.abcStock = abcStock; } @Override public void execute() { abcStock.sell(); } }
(4)定义命令调用类Broker
public class Broker { private List<Command> CommandList = new ArrayList<Command>(); public void takeCommand(Command Command){ CommandList.add(Command); } public void placeCommands(){ for (Command Command : CommandList) { Command.execute(); } CommandList.clear(); } }
(5)使用 Broker 类来接受并执行命令:
public class Main { public static void main(String[] args) { Stock abcStock = new Stock(); BuyStock buyStockOrder = new BuyStock(abcStock); SellStock sellStockOrder = new SellStock(abcStock); Broker broker = new Broker(); broker.takeCommand(buyStockOrder); broker.takeCommand(sellStockOrder); broker.placeCommands(); } }
结果:
Stock [ Name: ABC,Quantity: 10 ] bought
Stock [ Name: ABC,Quantity: 10 ] sold
三、访问者模式
四、解释器模式
五、策略模式
六、模板模式
七、观察者模式
八、迭代器模式
九、备忘录模式
十、状态模式
十一、中介者模式
以上是关于设计模式-行为型模式的主要内容,如果未能解决你的问题,请参考以下文章