ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

徒手撸设计模式-解释器模式

2022-06-29 01:31:15  阅读:154  来源: 互联网

标签:解释器 expression2 Expression expression1 interpreter 设计模式 type public 徒手


概念

解释器模式(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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有