读书人

天职链的应用

发布时间: 2012-09-10 11:02:32 作者: rapoo

职责链的应用

在系统中经常要接收各方面的指令,解析指令后,交付一个处理程序进行统一处理,在这种情况下,如何系统设计不合理,就会出现众多的if-else,代码混乱不易扩展维护,所以采用职责链模式比较适合了!

例如,游戏中可能接受硬件指令,网络指令,手机指令,甚至其他,这都需要统一设计处理程序。

职责链模式本身比较简单。
但是要注意他和Filter模式的区别,Filter模式在处理指令时,是所有队列里面的所有filter处理器都会对指令进行处理。
职责链模式在处理指令的时候是:只要一个处理器处理了,那么程序就返回了,不继续处理了。

职责链基类
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);

通过process进行处理,处理完毕返回!
其中的基类AbstractSingleGameState是状态模式的一个类型,下一个文章说一下状态模式的应用!

其中的
ActionFilterChainNode filter = StepPostFilterFactory.GetChain();

是Filter模式
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);    }


可以看出来他是连续处理的,直到filter链的最后一个处理器

现在看出来,如果要扩展处理器,只要实现一个ActionChainNode就可以了,然后挂到相应的职责链上就可以了,代码清晰,以维护,扩展性强。

完毕!
http://qixin000.iteye.com/blog/1491198

读书人网 >编程

热点推荐