现在的位置: 首页 > 综合 > 正文

Command模式实例(C#)

2013年08月02日 ⁄ 综合 ⁄ 共 2218字 ⁄ 字号 评论关闭

Command模式实例(C#)

 

用命令模式设计一个公告板系统模块:

 

某软件公司欲开发一个基于Windows平台的公告板系统。系统提供一个主菜单(Menu),在主菜单中包含了一些菜单项(MenuItem),可以通过Menu类的addMenuItem()方法增加菜单项。菜单项的主要方法是click(),每一个菜单项包含一个抽象命令类,具体命令类包括OpenCommand(打开命令),CreateCommand(新建命令),EditCommand(编辑命令)等,命令类具有一个execute()方法,用于调用公告板系统界面类(BoardScreen)的open()、create()、edit()等方法。现使用命令模式设计该系统,使得MenuItem类与BoardScreen类的耦合度降低,绘制类图并编程实现。

 

类图:

 

类图说明:

由类图可见,Command抽象类下面实现了三个具体的类,分别是CreateCommand类、EditCommand类和OpenCommand类。然后是一个Invoker——MenuItem类,Receiver——BoardScreen类。Program类可以作为客户端的调用。

 

各类源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommandMenu
{

    //receiver类
    public class BoardScreen
    {
        public void Open()
        {
            Console.WriteLine("Open Command Excute!");
        }
        public void Create()
        {
            Console.WriteLine("Create Command Excute!");
        }
        public void Edit()
        {
            Console.WriteLine("Edit Command Excute!");
        }
    }

    //Command类
    public abstract class Command
    {
        protected BoardScreen boardScreen;
        public Command(BoardScreen boardScreen)
        {
            this.boardScreen = boardScreen;
        }

        abstract public void Execute();
    }

    //具体的command类
    public class OpenCommand : Command
    {
        public OpenCommand(BoardScreen boardScreen) : base(boardScreen) { }
        public override void Execute()
        {
            //throw new NotImplementedException();
            boardScreen.Open();
        }
    }

    public class CreateCommand : Command
    {
        public CreateCommand(BoardScreen boardScreen) : base(boardScreen) { }
        public override void Execute()
        {
            //throw new NotImplementedException();
            boardScreen.Create();
        }
    }

    public class EditCommand : Command
    {
        public EditCommand(BoardScreen boardScreen)
            : base(boardScreen)
        { }
        public override void Execute()
        {
            //throw new NotImplementedException();
            boardScreen.Edit();
        }
    }
    //Invoker类
    public class MenuItem
    {
        private Command command;
        public void addMenuItem(Command command)
        {
            this.command = command;
        }
        public void click()
        {
            command.Execute();
        }
    }
    //客户端类
    class Program
    {
        static void Main(string[] args)
        {
            BoardScreen boardScreen = new BoardScreen();
            MenuItem menuItem = new MenuItem();

            //使用OpenCommand类
            Command openCommand = new OpenCommand(boardScreen);
            menuItem.addMenuItem(openCommand);
            menuItem.click();

            //使用CreateCommand类
            Command creatCommand=new CreateCommand(boardScreen);
            menuItem.addMenuItem(creatCommand);
            menuItem.click();

            //使用EditCommand类
            Command editCommand = new EditCommand(boardScreen);
            menuItem.addMenuItem(editCommand);
            menuItem.click();


            Console.ReadKey();
        }
    }
}

运行结果图:

 

直观关系图:

 

 

抱歉!评论已关闭.