当前位置: 首页 > news >正文

设计模式_解释器模式

解释器模式 

案例

角色

1 解释器基类                 (BaseInterpreter)

2 具体解释器1 2 3...        (Interperter 1 2 3 )

3 内容                              (Context)

4 用户                              (user)

       

  流程

(上下文) ---- 传给排好顺序的解释器(可以通过配置文件信息排序)

              ---- 得到(修改后的上下文) 

代码

角色 Context

// 内容
public class Context
{private string text = null;public Context(string text){this.text = text;}public void Set(string newText){text = newText;}public string Get(){return text;}}

角色 BaseInterpreter

// 解释器基类
public abstract class BaseInterpreter
{public abstract void Conversion(Context context);
}

角色 Interpreter1


public class Interpreter1 : BaseInterpreter
{public override void Conversion(Context context){// 1 改为 A string text = context.Get();char[] c = text.ToCharArray();for (int i = 0; i < c.Length; i++){if (c[i] == '1'){c[i] = 'A';}}context.Set(new string(c));}
}

角色 Interpreter2

        和1类似 仅仅 2=B

角色 Interpreter3 

        和1类似 仅仅 3=C

角色 用户

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class Player : MonoBehaviour
{// 显示出内容public GameObject obj = null;void Start(){// 上下文Context context = new Context("123456789");// 创建解码List<BaseInterpreter> interpreterList = new List<BaseInterpreter>();interpreterList.Add(new Interpreter1());interpreterList.Add(new Interpreter2());interpreterList.Add(new Interpreter3());// 开始解码foreach (var item in interpreterList){item.Conversion(context);}obj.GetComponent<Text>().text = context.Get();}}

运行结果

http://www.lryc.cn/news/171489.html

相关文章:

  • 【算法基础】数学知识
  • PDCA循环
  • Redis 缓存雪崩、缓存穿透、缓存击穿
  • Android Media3 ExoPlayer 开启缓存功能
  • MyBatis注解开发
  • C# Onnx Yolov8 Cls 分类
  • Fiddler常用的快键键
  • 【Linux】生产消费模型 + 线程池
  • 基于springboot+vue的爱心助农网站(前后端分离)
  • “华为杯”研究生数学建模竞赛2019年-【华为杯】D题:汽车行驶工况构建(附获奖论文和MATLAB代码实现)
  • v-cloak的作用和原理
  • pip pip3安装库时都指向python2的库
  • 和逸云 RK3229 如何进入maskrom强刷模式
  • 防静电离子风扇的应用及优点
  • git中无法使用方向键的问题
  • 负载均衡中间件---Nginx
  • Linux硬链接、软链接
  • React面试题总结(一)
  • 一句话设计模式12:适配器模式
  • iOS加固保护技术:保护你的iOS应用免受恶意篡改
  • 阿里云产品试用系列-云桌面电脑
  • vue3使用vue-virtual-scroller虚拟滚动遇到的问题
  • c#用Gnuplot画图源码
  • 【前端设计模式】之工厂模式
  • Hive 的函数介绍
  • 【Linux基础】第31讲 Linux用户和用户组权限控制命令(三)
  • html form表单高级用法
  • openssl升级
  • 【数据结构】图的遍历:广度优先(BFS),深度优先(DFS)
  • Mysql 学习总结(89)—— Mysql 库表容量统计