策略模式
1.抽象策略类
/*---------------------------------------------------------------- * 作 者 :姜 彦 * 项目名称 :Utility.Tool.Controller.Controller.AE240Tester.Strategy * 类 名 称 :SendStrategy * 命名空间 :Utility.Tool.Controller.Controller.AE240Tester.Strategy * CLR 版本 :4.0.30319.42000 * 创建时间 :2018/9/7 21:24:26 * 当前版本 :1.0.0.1 * My Email :jiangyan2008.521@gmail.com * jiangyan2008.521@qq.com * 描述说明: * * 修改历史: * ******************************************************************* * Copyright @ JiangYan 2018. All rights reserved. ******************************************************************* ------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Utility.Tool.Controller.Controller.AE240Tester.Strategy { /// <summary> /// 发送策略 /// </summary> public abstract class SendStrategy { public abstract void CreateFrame(params object[] objArray); } }
2.策略上下文
/*---------------------------------------------------------------- * 作 者 :姜 彦 * 项目名称 :Utility.Tool.Controller.Controller.AE240Tester.Strategy * 类 名 称 :SendContext * 命名空间 :Utility.Tool.Controller.Controller.AE240Tester.Strategy * CLR 版本 :4.0.30319.42000 * 创建时间 :2018/9/7 21:23:44 * 当前版本 :1.0.0.1 * My Email :jiangyan2008.521@gmail.com * jiangyan2008.521@qq.com * 描述说明: * * 修改历史: * ******************************************************************* * Copyright @ JiangYan 2018. All rights reserved. ******************************************************************* ------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Utility.Tool.Model.Model; namespace Utility.Tool.Controller.Controller.AE240Tester.Strategy { /// <summary> /// 发送帧上下文 /// </summary> public class SendContext { private SendStrategy _SendStrategy = null; /// <summary> /// 构造函数,传入一种具体策略 /// </summary> /// <param name="sendStrategy"></param> public SendContext(SendStrategy sendStrategy) { _SendStrategy = sendStrategy; } /// <summary> /// 上下文对客户端提供的操作接口 /// 执行相应的策略 /// </summary> /// <param name="objArray"></param> public void Execute(params object[] objArray) { if (_SendStrategy!=null) { _SendStrategy.CreateFrame(objArray); } } } }
3.具体策略
/*---------------------------------------------------------------- * 作 者 :姜 彦 * 项目名称 :Utility.Tool.Controller.Controller.AE240Tester.Strategy * 类 名 称 :SendParameterReportStrategy * 命名空间 :Utility.Tool.Controller.Controller.AE240Tester.Strategy * CLR 版本 :4.0.30319.42000 * 创建时间 :2018/9/12 11:04:34 * 当前版本 :1.0.0.1 * My Email :jiangyan2008.521@gmail.com * jiangyan2008.521@qq.com * 描述说明: * * 修改历史: * ******************************************************************* * Copyright @ JiangYan 2018. All rights reserved. ******************************************************************* ------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Utility.Tool.Controller.Controller.AE240Tester.Strategy { /// <summary> /// SendParameterReportStrategy /// </summary> public class SendParameterReportStrategy:SendStrategy { public override void CreateFrame(params object[] objArray) { AE240PLCSendCtrl.GetInstance().OnRecvParamReportedEventHandler(100); } } }
4.策略工厂
/*---------------------------------------------------------------- * 作 者 :姜 彦 * 项目名称 :Utility.Tool.Controller.Controller.AE240Tester.Strategy * 类 名 称 :StrategyFactory * 命名空间 :Utility.Tool.Controller.Controller.AE240Tester.Strategy * CLR 版本 :4.0.30319.42000 * 创建时间 :2018/9/7 21:25:20 * 当前版本 :1.0.0.1 * My Email :jiangyan2008.521@gmail.com * jiangyan2008.521@qq.com * 描述说明: * * 修改历史: * ******************************************************************* * Copyright @ JiangYan 2018. All rights reserved. ******************************************************************* ------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Reflection; using System.Text; using Utility.Tool.Model.Model; namespace Utility.Tool.Controller.Controller.AE240Tester.Strategy { /// <summary> /// 策略工厂 /// </summary> public class StrategyFactory { public static SendStrategy CreateConcreteStrategy(CommandID cmdID) { SendStrategy recvStrategy = null; string key = cmdID.ToString(); string dlltype = ConfigurationManager.AppSettings[key]; if (!string.IsNullOrWhiteSpace(dlltype)) { Assembly assembly = Assembly.Load(dlltype.Split(',')[0]); Type type = assembly.GetType(dlltype.Split(',')[1]); if (type != null) { recvStrategy = (SendStrategy)Activator.CreateInstance(type); } } return recvStrategy; } } }
5.配置文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/> </startup> <appSettings> <add key="MSG_ANALYZE_START" value="Utility.Tool.Controller,Utility.Tool.Controller.Controller.AE240Tester.Strategy.SendRackBarcodeReportStrategy"/> <add key="MSG_EMERGENCYSTOP" value="Utility.Tool.Controller,Utility.Tool.Controller.Controller.AE240Tester.Strategy.SendEmergencyStopReportStrategy"/> <add key="MSG_START_TEST" value="Utility.Tool.Controller,Utility.Tool.Controller.Controller.AE240Tester.Strategy.SendStartTestReportStrategy"/> <add key="MSG_RACK_MOVE" value="Utility.Tool.Controller,Utility.Tool.Controller.Controller.AE240Tester.Strategy.SendRackMoveReportStrategy"/> <add key="MSG_RACK_PUSHOUT1" value="Utility.Tool.Controller,Utility.Tool.Controller.Controller.AE240Tester.Strategy.SendRackPushOutReportStrategy"/> <add key="MSG_SENDPARAMETER" value="Utility.Tool.Controller,Utility.Tool.Controller.Controller.AE240Tester.Strategy.SendParameterReportStrategy"/> <add key="DelayTime" value="100"/> </appSettings> </configuration>
浙公网安备 33010602011771号