命令模式

/*
 * 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(){}
        
public override void Execute()
        {
            Pen p
=new Pen(Color.Red);
            
this.cav.DrawLine(p,0,0,100,100);
        }
    }

    
public class RectCommand:Command{
        
public RectCommand(){}

        
public override void Execute()
        {
            Pen p
=new Pen(Color.Red);
            
this.cav.DrawRectangle(p,0,0,100,100);
        }
    }


    
public class SecurityCommand:Command{
        
private Command c;

        
public SecurityCommand(Command c){
            
this.c=c;
        }

        
public override void Execute()
        {
            MessageBox.Show(
"权限判断"+c.GetType().Name);
            c.Execute();
        }
    }
}


//界面的函数
//
/*

 * User: Administrator
 * Date: 2007-7-2 Time: 15:51
 
*/

using System;
using System.Drawing;
using System.Windows.Forms;
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();
        }
    }
}
 
上一篇:如何使用委托与事件来实现观察者模式


下一篇:Android开发4——文件操作模式