• 设计模式复习-解释器模式


    #pragma once
    #include "stdafx.h"
    #include<map>
    #include<set>
    #include<list>
    #include<string>
    #include<iostream>
    using namespace std;
    
    /*
    	设计模式-解释器模式(interpreter)
    	给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。
    如果一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表示为一个简单语言中的句子。
    这样就可以构建一个解释器,该解释器通过解释这些句子来解决问题(比如正则表达式)。
    */
    
    class CContext {//解释器之外的一些全局信息
    public:
    	string mstrInput;
    	CContext(const string &strInput) {
    		mstrInput = strInput;
    	}
    };
    
    class CAbstractExpression {//抽象解释操作
    public:
    	virtual void Interpret(CContext *pContext) = 0;
    };
    
    class ExpressionA : public CAbstractExpression {//表达式A
    public:
    	void Interpret(CContext *pContext) {
    		cout << "ExpressionA:" << pContext->mstrInput << endl;
    	}
    };
    
    class ExpressionB : public CAbstractExpression {//表达式B
    public:
    	void Interpret(CContext *pContext) {
    		cout << "ExpressionB:" << pContext->mstrInput << endl;
    	}
    };
    
    int main() {
    
    	list<CAbstractExpression*>lstWork;
    	lstWork.clear();
    	lstWork.push_back(new ExpressionA());
    	lstWork.push_back(new ExpressionB());
    	lstWork.push_back(new ExpressionA());
    	lstWork.push_back(new ExpressionB());
    
    	CContext *pContext = new CContext("test");
    	for each(CAbstractExpression * index in lstWork) {
    		index->Interpret(pContext);
    		delete index;
    	}
    	delete pContext;
    	getchar();
    	return 0;
    }

  • 相关阅读:
    Movement Type
    Excel制表技巧
    [转]MM移动类型的配置相关的系统表,举例说明SAP是如何根据配置抛帐的
    Microsoft Excel:Automatically color alternating rows(columns)
    eBook试载(1):Workbook对象与示例(1)
    辨析:would rather 和 prefer
    Serial Number Management In SAP SD
    <摘录>NAL格式
    linux服务简介关闭不需要的Linux服务
    <摘录>PS和TS流的区别
  • 原文地址:https://www.cnblogs.com/csnd/p/12061904.html
Copyright © 2020-2023  润新知