C# 设计模式-解释器(Interpreter)
2018-01-05 20:45
621 查看
给定一个语言,定义它文法的一种表示,并定义一个解释器,用于解释该表示。
主要组成:
AbstractExpression-抽象表达式
TerminalExpression-终止符表达式
NonterminalExpression-非终止符表达式
Context-上下文
相关类:
using System;
namespace Interpreter
{
/// <summary>
/// 表达式父类或接口
/// </summary>
abstract class ExpressionClass
{
public abstract bool Interpret();
}
/// <summary>
/// 布尔表达式
/// </summary>
class BooleanExp : ExpressionClass
{
//上下文
string Context;
public BooleanExp(string context)
{
Context = context;
}
public override bool Interpret()
{
return Context.ToLower() == "true";
}
}
/// <summary>
/// 或表达式
/// </summary>
class OrExp : ExpressionClass
{
BooleanExp Exp1;
BooleanExp Exp2;
public OrExp(BooleanExp exp1, BooleanExp exp2)
{
Exp1 = exp1;
Exp2 = exp2;
}
public override bool Interpret()
{
return Exp1.Interpret() || Exp2.Interpret();
}
}
/// <summary>
/// 与表达式
/// </summary>
class AndExp : ExpressionClass
{
BooleanExp Exp1;
BooleanExp Exp2;
public AndExp(BooleanExp exp1, BooleanExp exp2)
{
Exp1 = exp1;
Exp2 = exp2;
}
public override bool Interpret()
{
return Exp1.Interpret() && Exp2.Interpret();
}
}
}
调用:
using System;
namespace Interpreter
{
class Program
{
static void Main(string[] args)
{
//布尔表达式
BooleanExp booleanExp1 = new BooleanExp("true");
BooleanExp booleanExp2 = new BooleanExp("false");
//或表达式
OrExp orExp = new OrExp(booleanExp1, booleanExp2);
Console.WriteLine(orExp.Interpret());
//与表达式
AndExp andExp = new AndExp(booleanExp1, booleanExp2);
Console.WriteLine(andExp.Interpret());
Console.Read();
}
}
}
结果:
![](https://img-blog.csdn.net/20180105204430219?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTBfMzc4NjI0MDU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
参考资料:《设计模式-可复用面对对象软件的基础》
主要组成:
AbstractExpression-抽象表达式
TerminalExpression-终止符表达式
NonterminalExpression-非终止符表达式
Context-上下文
相关类:
using System;
namespace Interpreter
{
/// <summary>
/// 表达式父类或接口
/// </summary>
abstract class ExpressionClass
{
public abstract bool Interpret();
}
/// <summary>
/// 布尔表达式
/// </summary>
class BooleanExp : ExpressionClass
{
//上下文
string Context;
public BooleanExp(string context)
{
Context = context;
}
public override bool Interpret()
{
return Context.ToLower() == "true";
}
}
/// <summary>
/// 或表达式
/// </summary>
class OrExp : ExpressionClass
{
BooleanExp Exp1;
BooleanExp Exp2;
public OrExp(BooleanExp exp1, BooleanExp exp2)
{
Exp1 = exp1;
Exp2 = exp2;
}
public override bool Interpret()
{
return Exp1.Interpret() || Exp2.Interpret();
}
}
/// <summary>
/// 与表达式
/// </summary>
class AndExp : ExpressionClass
{
BooleanExp Exp1;
BooleanExp Exp2;
public AndExp(BooleanExp exp1, BooleanExp exp2)
{
Exp1 = exp1;
Exp2 = exp2;
}
public override bool Interpret()
{
return Exp1.Interpret() && Exp2.Interpret();
}
}
}
调用:
using System;
namespace Interpreter
{
class Program
{
static void Main(string[] args)
{
//布尔表达式
BooleanExp booleanExp1 = new BooleanExp("true");
BooleanExp booleanExp2 = new BooleanExp("false");
//或表达式
OrExp orExp = new OrExp(booleanExp1, booleanExp2);
Console.WriteLine(orExp.Interpret());
//与表达式
AndExp andExp = new AndExp(booleanExp1, booleanExp2);
Console.WriteLine(andExp.Interpret());
Console.Read();
}
}
}
结果:
参考资料:《设计模式-可复用面对对象软件的基础》
相关文章推荐
- [导入]C#面向对象设计模式纵横谈(16):(行为型模式) Interpreter 解释器模式.zip(9.14 MB)
- 步步为营 .NET 设计模式学习笔记 二十三、Interpreter(解释器模式)
- 设计模式循序渐进(9)解释器模式 Interpreter
- 设计模式 笔记 解释器模式 Interpreter
- 23设计模式之解释器模式(Interpreter)
- 设计模式(15)——解释器(Interpreter)
- Java设计模式-解释器模式(Interpreter)
- C#设计模式——解释器模式
- JAVA设计模式(23) —<行为型>解释器模式(Interpreter)
- 设计模式:解释器模式(Interpreter)
- Java设计模式--解释器模式【Interpreter Pattern】
- 面向对象设计模式之Interpreter解释器模式(行为型)
- 设计模式之(二十)解释器模式Interpreter
- 设计模式之解释器模式(Interpreter)
- 设计模式 解释器模式(Interpreter)
- 设计模式之解释器模式(interpreter)
- 设计模式之美:Interpreter(解释器)
- 设计模式:解释器模式(Interpreter)
- [设计模式]解释器(Interpreter)之大胆向MM示爱吧
- 设计模式之(二十)解释器模式Interpreter