赞
踩
解释器模式包含以下组件:
由于Unity项目中直接使用解释器模式的例子相对较少,我们可以构造一个简化的场景来演示其概念:
// 抽象表达式接口 public interface IExpression { bool Interpret(Context context); } // 终结符表达式 - 例如检查某个变量是否大于5 public class GreaterThanFive : IExpression { private string _variableName; public GreaterThanFive(string variableName) { _variableName = variableName; } public bool Interpret(Context context) { int value = context.GetVariable(_variableName); return value > 5; } } // 非终结符表达式 - 例如逻辑与操作 public class AndExpression : IExpression { private IExpression _left, _right; public AndExpression(IExpression left, IExpression right) { _left = left; _right = right; } public bool Interpret(Context context) { return _left.Interpret(context) && _right.Interpret(context); } } // 环境角色 - 提供上下文数据 public class Context { private Dictionary<string, int> _variables; public Context() { _variables = new Dictionary<string, int>(); } public void SetVariable(string name, int value) { _variables[name] = value; } public int GetVariable(string name) { if (_variables.ContainsKey(name)) return _variables[name]; else throw new KeyNotFoundException($"Variable {name} not found."); } } // 使用示例 var context = new Context(); context.SetVariable("score", 7); var isScoreGreaterThanFive = new GreaterThanFive("score"); var hasPowerUp = new GreaterThanFive("powerUpsCollected"); var andExpression = new AndExpression(isScoreGreaterThanFive, hasPowerUp); bool result = andExpression.Interpret(context); // 返回true,因为score > 5且powerUpsCollected > 5(假设已设置)
当然,以下是示例2至5的详细代码实现:
public class LessThanExpression : IExpression { private string _variableName; private int _threshold; public LessThanExpression(string variableName, int threshold) { _variableName = variableName; _threshold = threshold; } public bool Interpret(Context context) { int value = context.GetVariable(_variableName); return value < _threshold; } } // 使用: var lessThanExpression = new LessThanExpression("health", 30); context.SetVariable("health", 25); bool isHealthLow = lessThanExpression.Interpret(context); // 返回true,因为health < 30
public class OrExpression : IExpression { private IExpression _left, _right; public OrExpression(IExpression left, IExpression right) { _left = left; _right = right; } public bool Interpret(Context context) { return _left.Interpret(context) || _right.Interpret(context); } } // 使用: var hasScoreGreaterThanFive = new GreaterThanFive("score"); var hasHealthGreaterThanZero = new LessThanExpression("health", 0).Inverse(); var orExpression = new OrExpression(hasScoreGreaterThanFive, hasHealthGreaterThanZero); bool result = orExpression.Interpret(context); // 返回true,只要score > 5 或 health <= 0 其中一个条件满足
为了创建逻辑非表达式,我们首先需要为IExpression接口添加一个辅助方法Inverse()
以反转其结果。
public interface IExpression { bool Interpret(Context context); IExpression Inverse(); } public abstract class BaseExpression : IExpression { public abstract bool Interpret(Context context); public IExpression Inverse() { return new NotExpression(this); } } public class NotExpression : IExpression { private IExpression _innerExpression; public NotExpression(IExpression innerExpression) { _innerExpression = innerExpression; } public bool Interpret(Context context) { return !_innerExpression.Interpret(context); } public IExpression Inverse() { return _innerExpression; // 反转两次等于原表达式 } } // 使用: var notExpression = new GreaterThanFive("score").Inverse(); bool isScoreNotGreaterThanFive = notExpression.Interpret(context); // 返回true,当score <= 5时
public class EqualToExpression : IExpression { private string _variableName; private int _value; public EqualToExpression(string variableName, int value) { _variableName = variableName; _value = value; } public bool Interpret(Context context) { int value = context.GetVariable(_variableName); return value == _value; } } // 使用: var equalToExpression = new EqualToExpression("level", 10); context.SetVariable("level", 10); bool isLevelTen = equalToExpression.Interpret(context); // 返回true,因为level == 10
以上就是对解释器模式中的更多复杂表达式的实现。每个表达式都是上下文环境的一部分,并且可以被组合和嵌套来构建更复杂的逻辑结构。
python推荐学习汇总连接:
50个开发必备的Python经典脚本(1-10)
50个开发必备的Python经典脚本(41-50)
————————————————
最后我们放松一下眼睛
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。