首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

每天一个设计模式(命令模式 -2011.4.21 星期四)期待提问和讨论解决方法

2012-03-12 
每天一个设计模式(命令模式 -2011.4.21 星期四)期待提问和讨论先说点题外话:这几天的帖子基本全部纠结在星

每天一个设计模式(命令模式 -2011.4.21 星期四)期待提问和讨论
先说点题外话:

这几天的帖子基本全部纠结在星期一的帖子里一个朋友提出的需求了

这完全背离了我的初衷

发这个帖子的本意 是想和大家讨论寻求一些让我们在平时工作中所写的代码能具有更方便扩展和维护的方法。

而不是具体的去解决什么需求。

印象中每一本关于设计模式的书都会提到一句话

大概意思是“不要把自己的代码仅仅局限于实现功能”

实际工作中我也经常遇到这样的情况,当一个系统验收完成后用户使用了一段时间,一年,两年,或者更长

突然有一天 用户需要对软件的核心功能进行添加,或者其他的什么原因导致你需要重新拿起这个丢了很长时间的系统

去增加功能。这个时候就我个人而言大部分情况是不得不顺着代码先拟一遍系统的运行过程,及一些解决需求的思路

对我来说这是一个非常枯燥和痛苦的事情。

所以,有了一些想法,以至于后来有了这个帖子。面相对象的基本原则 封边-开放,到我们现在要讨论的设计模式,其实就我理解,设计模式的核心目的是对系统的功能进行解耦,最终目标是当我需要增加一个什么功能的时候我只需要关心我现在需要增加的功能,而不去关心以前的。

举个最简单的例子,就像电脑内部的内存条吗,而做为主板,我根本不需要关心内存条内部是怎么工作不管他是1g,还是2g又或是8m的内存,我只需要提供个接口让他 插进来 就可以了,既PNP,即插即用 囧

为什么我们的软件不能有这么方便的可扩展呢?这才是我这个帖子想要讨论和思考的


以下是今天的模式 命令模式 

没有关心实现,还是以前天的 薪水问题 做为案例。
代码比较简单,希望愿意看代码的人参与讨论

C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Salary s = new Salary();            Prize1 p1 = new Prize1();            Prize2 p2 = new Prize2();            Prize3 p3 = new Prize3();            Command c = new ConcreteCommand(s);            Invoker i = new Invoker();            i.SetCommand(c);            Console.WriteLine("基本工资:" + i.ExecuteCommand());            c = new ConcreteCommand(p1);            i.SetCommand(c);            Console.WriteLine("奖金1:" + i.ExecuteCommand());            c = new ConcreteCommand(p2);            i.SetCommand(c);            Console.WriteLine("奖金2:" + i.ExecuteCommand());            c = new ConcreteCommand(p3);            i.SetCommand(c);            Console.WriteLine("奖金3:" + i.ExecuteCommand());            Console.Read();        }    }    abstract class Command    {        protected Receiver _receiver;        public Command(Receiver receiver)        {            this._receiver = receiver;        }        abstract public string Execute();    }    class ConcreteCommand : Command    {        public ConcreteCommand(Receiver receiver)            : base(receiver)        {        }        public override string Execute()        {            return base._receiver.Action();        }    }    class Invoker    {        private Command _command;        public void SetCommand(Command cmd)        {            this._command = cmd;        }        public string ExecuteCommand()        {            return this._command.Execute();        }    }    class Receiver    {        public virtual string Action()        {            return "执行命令";        }    }    class Salary : Receiver    {        public override string Action()        {            return "1000";        }    }    class Prize1 : Receiver    {        public override string Action()        {            return "500";        }    }    class Prize2 : Receiver    {        public override string Action()        {            return "400";        }    }    class Prize3 : Receiver    {        public override string Action()        {            return "300";        }    }}



[解决办法]
似乎有点绕了
[解决办法]
C# code
static Invoker Salary = () => "1000"; 

热点排行