职责链的应用
public abstract class ActionChainNode { protected static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private ActionChainNode next; public ActionChainNode AddChain(ActionChainNode next) { this.next = next; return next; } public bool Process(IGameState gameState, Action action) { try { bool processFlag = Do(gameState, action); if (!processFlag && next != null) { processFlag = next.Process(gameState, action); return processFlag; } return processFlag; }catch(Exception ex) { log.Error(ex.Message,ex); } return false; } protected abstract bool Do(IGameState gameState, Action action); }internal class Action上分:ActionChainNode { private IPortService logic; private IPersistService persist; protected override bool Do(IGameState gameState, Action action) { logic = gameState.PortService; if (action.Sender == PlayPort.ControlPanel) { persist = gameState.PersistService; if (action.Command.Code == (short) CommandCode.上分) { ThreadPool.QueueUserWorkItem(new WaitCallback(Doo), action); return true; } } return false; } private void Doo(object state) { }}internal class Action心跳 : ActionChainNode { private IPortService logic; public Action心跳() { log.Info("**【启动】心跳!!"); Timer timer = new Timer(1000); timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); timer.Enabled = true; } protected override bool Do(IGameState gameState, Action action) { logic = gameState.PortService; return true; } void timer_Elapsed(object sender, ElapsedEventArgs e) { if(logic!=null) { ICommand heart = SingleCommand.Instance[CommandCode.心跳]; heart.Data = new int[] { }; logic.Write(new Action(PlayPort.Logic, PlayPort.ControlPanel, heart)); } } }public class GlobalActionChainFactory { private static GlobalActionChainFactory globalActionChainFactory = new GlobalActionChainFactory(); private ActionChainNode root; private GlobalActionChainFactory() { root = new Action1(); ActionChainNode next = root.AddChain(new Action2()) .AddChain(new Action3()).AddChain(new Action4()) .AddChain(new Action5()).AddChain(new Action6()) .AddChain(new Action7()); next.AddChain(new Action8()); } public static GlobalActionChainFactory Instance { get { return globalActionChainFactory; } } public static ActionChainNode GetChain() { return Instance.root; } }public class StepWaitingState : AbstractSingleGameState { public override GameState CurrentState { get { return GameState.StepWaiting; } } private Timer timer = new Timer(1000); private GameWait wait; public StepWaitingState() { timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); } public override object Enter(Action action) { try { Tag.Clear(); logic.Write(new Action(PlayPort.Logic, SingleCommand.SimpleCommand("等待期", "等待期"))); timer.Enabled = true; // wait = GameWait.Instance; wait.Waiting(); timer.Enabled = false; log.Debug("==========waiting finished =============="); // ActionFilterChainNode filter = StepPostFilterFactory.GetChain(); filter.Process(this, action); // ICommand command = SingleCommand.Instance[CommandCode.某指令]; command.Data = SingleGameContext.WinCard; logic.Write(new Action(LOGIC_PORT, command)); command.Data = SingleGameContext.WinCard.ToColorTextString(); logic.Write(new Action(LOGIC_PORT, UDP_PORT, command)); //等待界面回应 wait = GameWait.Instance; Tag.Add("open wait", wait); wait.Waiting(); Transit(action); } catch (Exception ex) { log.Error(ex.Message,ex); } return null; } protected override void OnPortEvent(object sender, Action action) { Doo(action); } private void Doo(object state) { Action action = (Action) state; ActionChainNode controlPanelActionChain = WatingActionChainFactory.GetChain(); controlPanelActionChain.Process(this, action); } public override IGameState Transit(Action action) { if (GameContext.GameState.CurrentState == CurrentState) GameContext.SetState(StateFactory.StepPost, action); return GameContext.GameState; } }ActionChainNode controlPanelActionChain = WatingActionChainFactory.GetChain();
controlPanelActionChain.Process(this, action);
ActionFilterChainNode filter = StepPostFilterFactory.GetChain();
public abstract class ActionFilterChainNode { protected static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private ActionFilterChainNode next; public ActionFilterChainNode AddChain(ActionFilterChainNode next) { this.next = next; return next; } public void Process(IGameState gameState, Action action) { try { Do(gameState, action); if (next != null) next.Process(gameState,action); }catch(Exception ex) { MessagePrompt.Alert(this,"出现异常!" + Environment.NewLine + ex.Message); } } protected abstract void Do(IGameState gameState, Action action); }