设计模式---翻译器模式

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApplication1
  5. {
  6.     abstract class Expretion
  7.     {
  8.         public void Translate(Words w)
  9.         {
  10.             string s = w.MyWords.Substring(0, 1);
  11.             w.MyWords = w.MyWords.Substring(2);
  12.             Excute(s);
  13.         }
  14.         public abstract void Excute(string s);
  15.     }
  16.     class EnglishExpretion:Expretion
  17.     {
  18.         public override void Excute(string s)
  19.         {
  20.             switch (s)
  21.             {
  22.                 case "e":
  23.                     Console.WriteLine("对方说的英语");
  24.                 break;
  25.             }
  26.         }
  27.     }
  28.     class GermanExpretion:Expretion
  29.     {
  30.         public override void Excute(string s)
  31.         {
  32.             switch (s)
  33.             {
  34.                 case "g":
  35.                     Console.WriteLine("对方说的德语");
  36.                     break;
  37.             }
  38.         }
  39.     }
  40.     class Words
  41.     {
  42.         private string words;
  43.         public string MyWords
  44.         {
  45.             get
  46.             {
  47.                 return words;
  48.             }
  49.             set
  50.             {
  51.                 words = value;
  52.             }
  53.         }
  54.     }
  55.     class Client
  56.     {
  57.         public static void Main()
  58.         {
  59.             Words w = new Words();
  60.             w.MyWords = "e g e e g ";
  61.             Expretion e=null;
  62.             while (w.MyWords.Length>0)
  63.             {
  64.                 switch (w.MyWords.Substring(0,1))
  65.                 {
  66.                 case "e":
  67.                         e = new EnglishExpretion();
  68.                     break;
  69.                     case"g":
  70.                     e = new GermanExpretion();
  71.                     break;
  72.                 }
  73.                 e.Translate(w);
  74.             }
  75.             Console.Read();
  76.         }
  77.     }
  78. }

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值