天天看点

命令模式

/*

 * user: administrator

 * date: 2007-7-2 time: 15:49

 */

//一些命令,作用是画圆,画方,画线,三个主要的命令,

//命令模式最主的特点是,可以方便的加入权限的判断

//最后便使用了一个代理模式,来实现这个权限的判断

//

using system;

using system.drawing;

using system.windows.forms;

namespace securitycommand{

    public abstract class command{

        private graphics g;

        public command(){}

        public graphics cav{

            get{return g;}

            set{g=value;}

        }

        public abstract void execute();

    }

    public class circlecommand:command{

        public circlecommand(){}

        public override void execute()

        {

            pen p=new pen(color.red);

            this.cav.drawellipse(p,0,0,100,100);

    public class linecommand:command{

        public linecommand(){}

            this.cav.drawline(p,0,0,100,100);

    public class rectcommand:command{

        public rectcommand(){}

            this.cav.drawrectangle(p,0,0,100,100);

    public class securitycommand:command{

        private command c;

        public securitycommand(command c){

            this.c=c;

            messagebox.show("权限判断"+c.gettype().name);

            c.execute();

}

//界面的函数

 * date: 2007-7-2 time: 15:51

using securitycommand;

namespace mymemonto

{

    /// <summary>

    /// description of mydraw.

    /// </summary>

    public partial class mydraw : form

    {

        private command circle;

        private command line;

        private command rect;

        private command scircle;

        private command sline;

        private command srect;

        [stathread]

        public static  void main(){

            application.run(new mydraw());

        public mydraw()

            //

            // the initializecomponent() call is required for windows forms designer support.

            initializecomponent();

            init();

            scircle=new securitycommand.securitycommand(circle);

            sline=new securitycommand.securitycommand(line);

            srect=new securitycommand.securitycommand(rect);

            // todo: add constructor code after the initializecomponent() call.

        void init(){

            circle=new securitycommand.circlecommand();

            circle.cav=this.creategraphics();

            line=new securitycommand.linecommand();

            line.cav=this.creategraphics();

            rect=new securitycommand.rectcommand();

            rect.cav=this.creategraphics();

        void tsmcircleclick(object sender, eventargs e)

            scircle.execute();

        void tsmlineclick(object sender, eventargs e)

            sline.execute();

        void tsmrectclick(object sender, eventargs e)

            srect.execute();

        void tsmexitclick(object sender, eventargs e)

            application.exit();

上一篇: 访问者模式
下一篇: 备忘录模式