画一个“使用命令模式设计一个图形绘制系统,该系统可以绘制矩形、椭圆、三角形等图形,恢复和撤销功能。绘制类图并编程实现。”的uml类图
时间: 2024-02-16 16:04:21 浏览: 97
好的,下面是我画的UML类图:
```
+-----------------+ +---------------------+
| Invoker | | Command |
+-----------------+ +---------------------+
| executeCommand()| <------ | execute() |
| addCommand() | | undo() |
| removeCommand() | +---------------------+
+-----------------+ /\
/\ |
| |
| |
| |
| |
+--------+-----------+ +--------+----------+
| Receiver | | Concrete |
+-------------------+ | CommandA |
| drawRectangle() | | CommandB |
| drawEllipse() | | CommandC |
| drawTriangle() | +--------------------+
| undoLastDraw() | | + draw() |
| redoLastDraw() | | + undo() |
+-------------------+ +--------------------+
```
其中,Invoker为调用者,Command为抽象命令类,Receiver为接受者,Concrete Command为具体命令类。在该图形绘制系统中,Invoker负责调用具体的Command对象,而Command对象则负责调用Receiver中的方法进行具体的绘制操作。Receiver则负责实现具体的绘制方法,并提供撤销和恢复绘制操作的方法。
编程实现可以参考以下示例代码:
```java
//命令接口
public interface Command {
void execute();
void undo();
}
//具体命令类-绘制矩形
public class DrawRectangleCommand implements Command {
private Receiver receiver;
public DrawRectangleCommand(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.drawRectangle();
}
@Override
public void undo() {
receiver.undoLastDraw();
}
}
//具体命令类-绘制椭圆
public class DrawEllipseCommand implements Command {
private Receiver receiver;
public DrawEllipseCommand(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.drawEllipse();
}
@Override
public void undo() {
receiver.undoLastDraw();
}
}
//具体命令类-绘制三角形
public class DrawTriangleCommand implements Command {
private Receiver receiver;
public DrawTriangleCommand(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.drawTriangle();
}
@Override
public void undo() {
receiver.undoLastDraw();
}
}
//接受者
public class Receiver {
private List<String> drawHistory = new ArrayList<>();
public void drawRectangle() {
//绘制矩形
drawHistory.add("rectangle");
}
public void drawEllipse() {
//绘制椭圆
drawHistory.add("ellipse");
}
public void drawTriangle() {
//绘制三角形
drawHistory.add("triangle");
}
public void undoLastDraw() {
//撤销上一次绘制
if (drawHistory.size() > 0) {
drawHistory.remove(drawHistory.size() - 1);
}
}
public void redoLastDraw() {
//恢复上一次绘制
}
}
//调用者
public class Invoker {
private List<Command> commandHistory = new ArrayList<>();
private Receiver receiver;
public Invoker(Receiver receiver) {
this.receiver = receiver;
}
public void executeCommand(Command command) {
command.execute();
commandHistory.add(command);
}
public void undoLastCommand() {
if (commandHistory.size() > 0) {
Command lastCommand = commandHistory.remove(commandHistory.size() - 1);
lastCommand.undo();
}
}
public void redoLastCommand() {
//恢复上一次操作
}
}
//客户端代码
public class Client {
public static void main(String[] args) {
Receiver receiver = new Receiver();
Invoker invoker = new Invoker(receiver);
Command commandA = new DrawRectangleCommand(receiver);
Command commandB = new DrawEllipseCommand(receiver);
Command commandC = new DrawTriangleCommand(receiver);
invoker.executeCommand(commandA);
invoker.executeCommand(commandB);
invoker.executeCommand(commandC);
invoker.undoLastCommand();
invoker.undoLastCommand();
}
}
```
阅读全文