aiyoudiao aiyoudiao
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档

码二

扫微信二维码,认识一下码二吧😉。
  • JavaScript
  • Vue
  • React
  • 低代码
  • 线性系统
  • 暂未分类
  • LeetCode
  • 算法
  • 数据结构
  • 设计模式
  • Other
  • PMP
  • Office
  • 面试
  • Bash
  • 流年往事
  • 经验片段
  • 读书杂感
  • 归档
  • 分类
  • 标签
  • 简介
  • 收藏
  • 有趣
  • 文档
  • LeetCode

  • 算法

  • 数据结构

  • 设计模式

    • 浅聊设计理念
    • 谈一谈设计原则
    • 工厂方法模式
    • 抽象工厂模式
    • 建造者模式
    • 原型模式
    • 单例模式
    • 桥接模式
    • 代理模式
    • 装饰器模式
    • 适配器模式
    • 享元模式
    • 组合模式
    • 外观模式
    • 观察者模式
    • 模板方法模式
    • 策略模式
    • 职责链模式
    • 状态模式
    • 迭代器模式
    • 访问者模式
    • 备忘录模式
    • 命令模式
    • 解释器模式
      • 前言
      • 解释器模式
    • 中介者模式
  • Other

  • vue3设计与实现

  • 算法与设计
  • 设计模式
aiyoudiao
2022-02-25

解释器模式

# 前言

解释器模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

# 解释器模式

使用场景:当需要自定义规则,并且支持自己解释这些规则时,可以使用这个设计模式,比如JS 解释器,先定义好了语法,你按照语法来写代码,然后通过解释器来运行这些代码,最终拿到结果。

理解:这是一种类、对象之间的经典交互方式,将类、对象的行为和使用解耦了。解释器用于自定义语言规则的场景,类似于计算器、翻译器、编译器等等。这种设计模式,一般偏向底层的开发场景,例如编译原理,只做了解即可,不过蛮好玩。

namespace action_mode_10 {

    interface IExpression {

        interpret(context: Context): void

    }

    class Context {

        private instructions: string = ''
        private list: Array<IExpression> = new Array()

        constructor(instructions: string) {
            this.instructions = instructions
        }

        set(instructions: string) {
            this.instructions = instructions
        }

        get(): string {
            return this.instructions
        }

        pushExperssion (expression: IExpression) {
            this.list.push(expression)
        }

        getList() {
            return this.list
        }

    }

    // 解释器1:非终结表达式类
    class NonterminalExpression implements IExpression {

        private map: Map<string, string> = new Map()

        constructor(map: Map<string, string>) {
            this.map = map
        }

        interpret(context: Context): void {
            const instructions = context.get()
            let newInstructions = ''
            let result = ''

            for (const text of instructions) {
                if (this.map.has(text)) {
                    result += this.map.get(text)
                    // console.log(`解释:${text} 答案为:${this.map.get(text)}`)
                } else {
                    newInstructions += text
                }
            }

            context.set(newInstructions)
            if (result) {
                console.log(`解释器解释完毕:答案为:${result}`)
            }
        }
    }

    // 解释器2:终结表达式类
    class TerminalExpression implements IExpression {

        private startflag: string
        private endflag: string

        constructor(startflag: string, endFlag: string) {
            this.startflag = startflag
            this.endflag = endFlag
        }

        interpret(context: Context): void {
            const instructions = context.get()

            const startIndex = instructions.indexOf(this.startflag)
            const startLength = this.startflag.length
            const endIndex = instructions.indexOf(this.endflag)
            const endLength = this.endflag.length

            let newInstructions = ''

            if (startIndex !== -1 && endIndex !== -1) {
                newInstructions = instructions.slice(startIndex + startLength, endIndex + endLength)
            } if (startIndex === -1 && endIndex !== -1) {
                newInstructions = instructions.slice(0, endIndex + endLength)
            } else if (startIndex !== -1 && endIndex === -1) {
                newInstructions = instructions.slice(startIndex + startLength)
            } else {
                newInstructions = instructions
            }

            context.set(newInstructions)
        }

    }

    // 使用1
    const context = new Context('$wwssaaddABABAABBBBAAq')
    const terminalExpression = new TerminalExpression('$', 'q')
    const map1 = new Map()
    .set('w', '上').set('s', '下')
    .set('a', '左').set('d', '右')
    .set('A', '攻击').set('B', '防御')
    const nonterminalExpression = new NonterminalExpression(map1)

    context.pushExperssion(terminalExpression)
    context.pushExperssion(nonterminalExpression)
    context.getList().forEach(expression => {
        expression.interpret(context)
    })

    // 再来一把
    context.set('$wsadABsdwaBAAAAAAAAAABBBBBBBBBB')
    context.getList().forEach(expression => {
        expression.interpret(context)
    })

}
#设计模式
上次更新时间: 10年18月2023日 01时57分53秒
命令模式
中介者模式

← 命令模式 中介者模式 →

最近更新
01
01.数据结构导论一览.md
10-16
02
30.2023年06月04日.md
06-04
03
08.与测量相关.md
05-06
更多文章>
Theme by Vdoing | Copyright © 2017-2023 aiyoudiao 码二 备案号: 鄂ICP备2022002654号-1