我学会了,解释器模式

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

前言

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

解释器模式

使用场景:当需要自定义规则,并且支持自己解释这些规则时,可以使用这个设计模式,比如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)
    })

}
目录
相关文章
|
1月前
|
设计模式
【设计模式】解释器模式
【设计模式】解释器模式
|
1月前
|
设计模式 监控 Java
聊聊Java设计模式-解释器模式
解释器模式(Interpreter Design Pattern)指给定一个“语言”,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。这里所指的“语言”是指使用规定格式和语法的代码。
51 4
聊聊Java设计模式-解释器模式
|
1月前
|
设计模式 存储 前端开发
【设计模式】之解释器模式
解释器模式是一种用于解释特定语言或规则的表达式的行为设计模式。在前端开发中,解释器模式可以用于处理复杂的逻辑或规则,并将其转化为可执行的代码。它具有灵活性和可扩展性的优点,但也存在复杂性和性能问题的缺点。通过合理地应用解释器模式,可以提高代码的可读性和可维护性,实现更灵活和可扩展的功能。
49 1
|
1月前
|
设计模式 SQL 自然语言处理
行为型 解释器模式
行为型 解释器模式
28 0
|
1月前
|
设计模式 SQL 应用服务中间件
设计模式之解释器模式
设计模式之解释器模式
|
8月前
|
设计模式 C++
设计模式之解释器模式(C++)
设计模式之解释器模式(C++)
|
8月前
|
设计模式 SQL Java
设计模式~解释器模式(Interpreter)-19
解释器模式(Interpreter Pattern)提供了评估语言的语法或表达式的方式,它属于行为型模式。这种模式实现了一个表达式接口,该接口解释一个特定的上下文。这种模式被用在 SQL 解析、符号处理引擎等。 目录 (1)优点: (2)缺点: (3)使用场景: (4)注意事项: (5)应用实例: 代码
53 0
|
XML 设计模式 JSON
设计模式轻松学【二四】解释器模式
在生活中我们往往没听懂别人说的话,需要别人详细的解释一遍,才明白其中的意思。在学习Java的过程中我们不明白某个知识点,也需要有详细的解释才能让我们明白其中的韵味。对于编程语言来说也一样,JDK就是java运行提供的一种解释器,所以解释器模式就是去解析某个东西。
94 0
设计模式轻松学【二四】解释器模式
|
设计模式 Java
Java设计模式 ->解释器模式
Java设计模式 ->解释器模式
70 0
|
Java 编译器 数据安全/隐私保护
中介者模式与解释器模式(2)
中介者模式与解释器模式(2)
103 0
中介者模式与解释器模式(2)