极速理解设计模式系列
来源:互联网 发布:linux wget安装 编辑:程序博客网 时间:2024/05/18 23:28
2012-01-09 10:59:00
标签:接口 客户端 表达式 解释器 极速
- //场景(Context)
- class Context
- {
- public Context(string input)
- {
- this.Input = input;
- }
- /// <summary>
- /// 输入参数
- /// </summary>
- public string Input { get; set; }
- /// <summary>
- /// 输出参数
- /// </summary>
- public int Output { get; set; }
- /// <summary>
- /// 是否是十六进制 如果是转为十进制,否则不转
- /// </summary>
- public bool Status { get; set; }
- }
- //抽象表达式类(AbstractExpression)
- abstract class Expression
- {
- public virtual void Interpret(Context context)
- {
- if (context.Input.Length == 0)
- return;
- int multiresult = Multiplier(context);
- if (multiresult == 0)
- return;
- if(context.Input.StartsWith("F"))
- {
- context.Output += (15 * multiresult);
- context.Input=context.Input.Substring(1);
- }
- else if (context.Input.StartsWith("F"))
- {
- context.Output += (15 * multiresult);
- context.Input = context.Input.Substring(1);
- }
- else if (context.Input.StartsWith("E"))
- {
- context.Output += (14 * multiresult);
- context.Input = context.Input.Substring(1);
- }
- else if (context.Input.StartsWith("D"))
- {
- context.Output += (13 * multiresult);
- context.Input = context.Input.Substring(1);
- }
- else if (context.Input.StartsWith("C"))
- {
- context.Output += (12 * multiresult);
- context.Input = context.Input.Substring(1);
- }
- else if (context.Input.StartsWith("B"))
- {
- context.Output += (11 * multiresult);
- context.Input = context.Input.Substring(1);
- }
- else if (context.Input.StartsWith("A"))
- {
- context.Output += (10 * multiresult);
- context.Input = context.Input.Substring(1);
- }
- else
- {
- context.Output += (int.Parse(context.Input.Substring(0, 1)) * multiresult);
- context.Input = context.Input.Substring(1);
- }
- }
- //该位置需要做的乘法值
- public abstract int Multiplier(Context context);
- }
- //终结符表达式(TerminalExpression)
- class NumterminalExp : Expression
- {
- public override void Interpret(Context context)
- {
- if (context.Input.StartsWith("0X"))
- {
- context.Input = context.Input.Substring(2);
- context.Status = true;
- }
- else
- {
- context.Output = int.Parse(context.Input);
- context.Status = false;
- return;
- }
- }
- public override int Multiplier(Context context)
- {
- return 1;
- }
- }
- //非终结符表达式(NonterminalExpression) 千位计算
- class ThousandExp : Expression
- {
- public override int Multiplier(Context context)
- {
- if (context.Input.Length == 4&&context.Status)
- return 16 * 16 * 16;
- else
- return 0;
- }
- }
- //非终结符表达式(NonterminalExpression) 百位计算
- class HundredExp : Expression
- {
- public override int Multiplier(Context context)
- {
- if (context.Input.Length == 3 && context.Status)
- return 16 * 16;
- else
- return 0;
- }
- }
- //非终结符表达式(NonterminalExpression) 十位计算
- class TenExp : Expression
- {
- public override int Multiplier(Context context)
- {
- if (context.Input.Length == 2 && context.Status)
- return 16;
- else
- return 0;
- }
- }
- //非终结符表达式(NonterminalExpression) 个位计算
- class OneExp : Expression
- {
- public override int Multiplier(Context context)
- {
- if (context.Input.Length == 1 && context.Status)
- return 1;
- else
- return 0;
- }
- }
- //客户端(Client)
- class Program
- {
- static void Main(string[] args)
- {
- string input = "0XA321";
- Context context = new Context(input.ToUpper());
- List<Expression> expTree = new List<Expression>();
- expTree.Add(new NumterminalExp());
- expTree.Add(new ThousandExp());
- expTree.Add(new HundredExp());
- expTree.Add(new TenExp());
- expTree.Add(new OneExp());
- foreach (Expression exp in expTree)
- {
- exp.Interpret(context);
- }
- Console.WriteLine("十六进制数{0}转换为十进制数{1}", input, context.Output);
- Console.ReadLine();
- }
- }
- 极速理解设计模式系列
- 极速理解设计模式系列
- 极速理解设计模式系列:1.建造者模式(Builder Pattern)
- 极速理解设计模式系列:2.工厂方法模式(Factory Method Pattern)
- 极速理解设计模式系列:3.简单工厂模式(Simple Factory Pattern)
- 极速理解设计模式系列:4.抽象工厂模式(Abstract Factory Pattern)
- 极速理解设计模式系列:5.单例模式(Singleton Pattern)
- 深入理解JavaScript系列之设计模式
- 深入理解JavaScript系列(29):设计模式之装饰者模式
- 深入理解JavaScript系列(25):设计模式之单例模式
- 深入理解JavaScript系列(26):设计模式之构造函数模式
- 深入理解JavaScript系列(27):设计模式之建造者模式
- 深入理解JavaScript系列(28):设计模式之工厂模式
- 深入理解JavaScript系列(29):设计模式之装饰者模式
- 深入理解JavaScript系列(30):设计模式之外观模式
- 深入理解JavaScript系列(31):设计模式之代理模式
- 深入理解JavaScript系列(32):设计模式之观察者模式
- 深入理解JavaScript系列(33):设计模式之策略模式
- 1588: [HNOI2002]营业额统计 (splay模板)
- 原理分析之三:初始化(配置文件读取和解析)
- vim学习(二)移动+修改文本+多行注释等
- 字符串基础NSString
- inno setup打包实例
- 极速理解设计模式系列
- <<单例设计模式>>--黑马程序员
- 数组的最长连续的序列
- ubuntu安装jdk出错
- MS13-059分析
- C程序DLL调用。砸七烂八的C。
- 关于UITextField
- Weblogic9.x部署中遇到的一个问题
- Ufw使用指南