C # Design Patterns: interpretation mode (Interpreter Pattern)

A, C # Design Patterns: interpretation mode (Interpreter Pattern)

1 to explain the mode of application is the difficulty Interpreter mode applications, only to meet the "business rules change frequently, and a similar pattern constantly repeated, and easily abstracted into question the grammar rules" only suitable interpreter mode
2, interpretation design patterns explained each class has its own rules and does not conflict with other business rules

Second, the following code

the using the System;
 the using the System.Collections.Generic;
 the using the System.Linq;
 the using the System.Text;
 the using System.Threading.Tasks; 

namespace . _23 interpretation mode 
{ 
    ///  <Summary> 
    /// Interpreter Interpreter mode applications is difficulty mode applications, only to meet the "business rules change frequently, and a similar pattern is repeated constantly, and is easy to abstract syntax rules for the issue of" only suitable for use Interpreter mode.
    ///   1, when a desired language interpreted, and can be expressed in the language of the sentence as an abstract syntax tree, consider using an interpreter mode (such as an XML document explains, regular expressions and other fields)
     ///   2, some of the recurring problem can be a simple language to express.
    ///   3, the grammar of a language is relatively simple.
     ///   4, when the problem is not the key to efficiency and can be considered a major concern interpreter mode (Note: The effective interpreter is not usually accomplished by direct interpretation of the abstract syntax tree , but need to convert them into other forms, using an interpreter mode execution efficiency is not high.)
     ///  </ Summary>
    class Program
    {
        static void Main(string[] args)
        {
            Context context = new Context("usachi");
            List<PeopleInterpreter> interpreterList = new List<PeopleInterpreter>()
                    {
                        new Chinese(),
                        new Usa(),
                    };
            foreach (var item in interpreterList)
            {
                item.Conversion(context);
            }
            Console.WriteLine(context.Get());
        }
    }

    /// <summary>
    /// 上下文
    /// </summary>
    public class Context
    {
        private string _Word = null;
        public Context(string word)
        {
            this._Word = word;
        }

        public void Set(string newWord)
        {
            this._Word = newWord;
        }

        public string Get()
        {
            return this._Word;
        }
    }

    /// <summary>
    /// 抽象解释器
    /// </summary>
    public abstract class PeopleInterpreter
    {
        public abstract void Conversion(Context context);
    }
    /// <summary>
    /// 中国人业务
    /// </summary>
    public class Chinese : PeopleInterpreter
    {
        private static Dictionary<char, string> _Dictionary = newThe Dictionary <char, String > ();
         ///  <the Summary> 
        /// China's own interpretation of the rules
         ///  </ the Summary> 
        static Chinese () 
        { 
            _Dictionary.Add ( ' U ' , " Americans " ); 
            _Dictionary.Add ( ' S ' , ' knife and fork " ); 
            _Dictionary.Add ( ' A ' , " eat, " );
        }

        public override void Conversion(Context context)
        {
            Console.WriteLine(this.GetType().ToString() + "业务");
            string text = context.Get();
            if (string.IsNullOrEmpty(text))
                return;
            List<string> numberList = new List<string>();
            foreach (var item in text.ToLower().ToArray())
            {
                if (_Dictionary.ContainsKey(item))
                {
                    numberList.Add(_Dictionary[item]);
                }
                else
                {
                    numberList.Add(item.ToString());
                }
            }
            context.Set(string.Concat(numberList));
        }
    }
    /// <summary>
    /// 美国人业务
    /// </summary>
    public class Usa : PeopleInterpreter
    {
        private static Dictionary<char, string> _Dictionary = new Dictionary<char, String > ();
         ///  <the Summary> 
        /// Americans own rules of interpretation
         ///  </ the Summary> 
        static Usa () 
        { 
            _Dictionary.Add ( ' c ' , " Chinese people " ); 
            _Dictionary.Add ( ' H ' , ' a ' ); 
            _Dictionary.Add ( ' I ' , ' chopsticks " );
        }

        public override void Conversion(Context context) 
        { 
            Console.WriteLine ( the this . .GetType () the ToString () + "business");
            string text = context.Get();
            if (string.IsNullOrEmpty(text))
                return;
            List<string> numberList = new List<string>();
            foreach (var item in text.ToLower().ToArray())
            {
                if (_Dictionary.ContainsKey(item))
                {
                    numberList.Add(_Dictionary[item]);
                }
                else
                {
                    numberList.Add(item.ToString());
                }
            }
            context.Set(string.Concat(numberList));
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/May-day/p/11718888.html