徒手撸设计模式-解释器模式
作者:互联网
概念
解释器模式(Interpreter Pattern)提供了评估语言的语法或表达式的方式,它属于行为型模式。这种模式实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等。
参考链接: https://www.runoob.com/design-pattern/interpreter-pattern.html
代码案例
新增表达式接口
/** * 表达式接口 */ public interface Expression { boolean interpreter(String type); }
表达式实现类
生产表达式
/** * 生产表达式 */ public class ProductionExpression implements Expression { private String data; public ProductionExpression(String data) { this.data = data; } @Override public boolean interpreter(String type) { if (type.contains(data)){ return true; } return false; } }
自行车或红色表达式
/** * 自行车或红色表达式 */ public class BicycleOrRedExpression implements Expression { Expression expression1= null; Expression expression2= null; public BicycleOrRedExpression(Expression expression1, Expression expression2) { this.expression1 = expression1; this.expression2 = expression2; } @Override public boolean interpreter(String type) { return expression1.interpreter(StFlag.BICYCLE.toLowerCase()) || expression2.interpreter(StFlag.RED.toLowerCase()) ; } }
蓝色汽车表达式
/** * 蓝色汽车表达式 */ public class CarAndBlueExpression implements Expression { Expression expression1= null; Expression expression2= null; public CarAndBlueExpression(Expression expression1, Expression expression2) { this.expression1 = expression1; this.expression2 = expression2; } @Override public boolean interpreter(String type) { return expression1.interpreter(StFlag.CAR.toLowerCase()) && expression2.interpreter(StFlag.BLUE.toLowerCase()) ; } }
表达式实体类
@Setter @Getter public class InterpreterEntity { private String type; private String colour; }
测试主类
/** * 设计模式控制器 */ @RestController @RequestMapping("/designPattern") @Slf4j public class DesignController { @PostMapping("/interpreter") public ResponseModel interpreter(@RequestBody List<InterpreterEntity> interpreterEntityList) { log.info("interpreter ---- start "); List list= new ArrayList(); for (InterpreterEntity interpreterEntity : interpreterEntityList) { String type = interpreterEntity.getType(); ProductionExpression typeExpression = new ProductionExpression(type); String colour = interpreterEntity.getColour(); ProductionExpression colourExpression = new ProductionExpression(colour); BicycleOrRedExpression bicycleOrRedExpression = new BicycleOrRedExpression(typeExpression, colourExpression); boolean interpreter = bicycleOrRedExpression.interpreter(type+colour); list.add(type+" or "+colour+"===bicycleOrRed==="+"---------"+interpreter); CarAndBlueExpression carAndBlueExpression = new CarAndBlueExpression(typeExpression, colourExpression); boolean interpreter1 = carAndBlueExpression.interpreter(type+colour); list.add(type+" And "+colour+"===carAndBlue==="+"---------"+interpreter1); } log.info("interpreter ---- end "); return new ResponseModel("命令模式完成", 200, list); } }
测试案例
2022-06-29 01:12:59.273 INFO interpreter ---- start 【http-nio-8081-exec-4】【DesignController:74】 2022-06-29 01:12:59.273 INFO bicycle Or 白色===bicycleOrRed===---------true 【http-nio-8081-exec-4】【DesignController:84】 2022-06-29 01:12:59.274 INFO bicycle And 白色===carAndBlue===---------false 【http-nio-8081-exec-4】【DesignController:88】 2022-06-29 01:12:59.274 INFO car Or blue===bicycleOrRed===---------false 【http-nio-8081-exec-4】【DesignController:84】 2022-06-29 01:12:59.274 INFO car And blue===carAndBlue===---------true 【http-nio-8081-exec-4】【DesignController:88】 2022-06-29 01:12:59.274 INFO interpreter ---- end 【http-nio-8081-exec-4】【DesignController:91】
标签:解释器,expression2,Expression,expression1,interpreter,设计模式,type,public,徒手 来源: https://www.cnblogs.com/hikoukay/p/16421838.html