问题
Sunny软件公司欲开发一个基于Windows平台的公告板系统。该系统提供了一个主菜单(Menu),在主菜单中包含了一些菜单项(MenuItem),可以通过Menu类的addMenuItem()方法增加菜单项。菜单项的主要方法是click(),每一个菜单项包含一个抽象命令类,具体命令类包括OpenCommand(打开命令),CreateCommand(新建命令),EditCommand(编辑命令)等,命令类具有一个execute()方法,用于调用公告板系统界面类(BoardScreen)的open()、create()、edit()等方法。试使用命令模式设计该系统,以便降低MenuItem类与BoardScreen类之间的耦合度。
package com.liyajie.test.CommandPattern.announcement;
/**
* 抽象命令类:请求发送者
* @author 李亚杰
* @date 2024/4/18 8:31
* @description AbstractCommand
*/
public abstract class AbstractCommand {
abstract protected void execute();
}
package com.liyajie.test.CommandPattern.announcement;
/**
* 请求接受者
* @author 李亚杰
* @date 2024/4/18 8:36
* @description BoardScreen
*/
public class BoardScreen {
public void open(){
System.out.println("执行打开命令");
}
public void create(){
System.out.println("执行创建命令");
}
public void edit(){
System.out.println("执行编辑命令");
}
}
package com.liyajie.test.CommandPattern.announcement;
/**
* 请求接受者
* @author 李亚杰
* @date 2024/4/18 8:59
* @description DleHandle
*/
public class DleHandle {
public void delete(){
System.out.println("执行删除功能");
}
}
package com.liyajie.test.CommandPattern.announcement;
/**
* 具体命令类 创建请求发送者
* @author 李亚杰
* @date 2024/4/18 8:32
* @description CreateCommand
*/
public class CreateCommand extends AbstractCommand{
private BoardScreen boardScreen= new BoardScreen();
@Override
protected void execute() {
boardScreen.create();
}
}
package com.liyajie.test.CommandPattern.announcement;
/**
* @author 李亚杰
* @date 2024/4/18 8:59
* @description DeleteCommand
*/
public class DeleteCommand extends AbstractCommand{
private DleHandle dleHandle = new DleHandle();
@Override
protected void execute() {
dleHandle.delete();
}
}
package com.liyajie.test.CommandPattern.announcement;
/**
* @author 李亚杰
* @date 2024/4/18 8:33
* @description EditCommand
*/
public class EditCommand extends AbstractCommand{
private BoardScreen boardScreen= new BoardScreen();
@Override
protected void execute() {
boardScreen.edit();
}
}
package com.liyajie.test.CommandPattern.announcement;
/**
* @author 李亚杰
* @date 2024/4/18 8:32
* @description OpenCommand
*/
public class OpenCommand extends AbstractCommand{
private BoardScreen boardScreen= new BoardScreen();
@Override
protected void execute() {
boardScreen.open();
}
}
package com.liyajie.test.CommandPattern.announcement;
/**
* 菜单项,设置请求
* @author 李亚杰
* @date 2024/4/18 8:44
* @description MenuItem
*/
public class MenuItem {
private String name;
private AbstractCommand command;
public MenuItem(String name) {
this.name = name;
}
public void setCommand(AbstractCommand command) {
this.command = command;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void onClick(){
System.out.println("点击功能键:"+name);
}
}
package com.liyajie.test.CommandPattern.announcement;
import java.util.HashMap;
import java.util.Map;
/**
* 菜单,添加各种功能
* @author 李亚杰
* @date 2024/4/18 8:46
* @description Menu
*/
public class Menu {
private Map<String,MenuItem> menuItemMap = new HashMap<>();
public void addMenuItem(String name,MenuItem menuItem){
if (menuItemMap.get(name)==null){
menuItemMap.put(name,menuItem);
System.out.println("功能键:"+name+"已添加,对应功能"+menuItem.getName());
return;
}
System.out.println("该键已被使用");
}
public void display(String name){
menuItemMap.get(name).onClick();
}
}
测试
package com.liyajie.test.CommandPattern.announcement;
/**
* @author 李亚杰
* @date 2024/4/18 8:53
* @description Clint
*/
public class Clint {
public static void main(String[] args) {
AbstractCommand cmd1,cmd2,cmd3;
cmd1 = new CreateCommand();
cmd2 = new OpenCommand();
cmd3 = new EditCommand();
MenuItem m1,m2,m3;
m1 = new MenuItem("打开功能");
m2 = new MenuItem("创建功能");
m3 = new MenuItem("编辑功能");
m1.setCommand(cmd2);
m2.setCommand(cmd1);
m3.setCommand(cmd3);
Menu menu = new Menu();
menu.addMenuItem("ctrl",m1);
menu.addMenuItem("ins",m2);
menu.addMenuItem("edit",m3);
menu.display("ctrl");
AbstractCommand cmd4 = new DeleteCommand();
MenuItem m4 = new MenuItem("删除");
m4.setCommand(cmd4);
menu.addMenuItem("del",m4);
menu.display("del");
}
}
测试结果
功能键:ctrl已添加,对应功能打开功能
功能键:ins已添加,对应功能创建功能
功能键:edit已添加,对应功能编辑功能
点击功能键:打开功能
功能键:del已添加,对应功能删除
点击功能键:删除