iT邦幫忙

0

【C#】Behavioral Patterns Interpreter Mode

c#
  • 分享至 

  • xImage
  •  

Given a language, the Interpreter design pattern defines a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.


簡單來說~ 解釋器能定義幾個解釋的方法~ 然後再將要解釋的方法丟進去解釋~


學習目標: 解釋器模式的概念及實務

學習難度: ☆☆☆


using System;

using System.Collections.Generic;

namespace ConsoleApp1
{
    public class Player
    {
        public string name="賈斯汀";
    }

    //聲明一個用於執行操作的接口
    public abstract class AbstractExpression
    {
        public abstract void Interpret(Player player);
    }

    //實體翻譯方法
    public class EnglishExpression : AbstractExpression
    {
        public override void Interpret(Player player)
        {
            player.name = "Justin";

            Console.WriteLine(player.name);
        }
    }

    public class MainProgram
    {
        public static void Main(string[] args)
        {
            Player player = new Player();

            Console.WriteLine(player.name);

            AbstractExpression englishExpression = new EnglishExpression();

            englishExpression.Interpret(player);
        }
    }
}


參考資料:

https://www.dofactory.com/net/interpreter-design-pattern


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言