使用命令模式设计一个图形绘制系统该系统可以绘制矩形椭圆三角形等图形恢复和撤销功能绘制类图并编程实现
时间: 2024-06-08 19:05:34 浏览: 172
C++绘图程序,绘制矩形,直线,曲线,三角形,多边形,椭圆,圆;给图形上色,框选图形,文件存储,坐标显示等等功能
首先,我们可以定义一个抽象命令类 `Command`,该类包含了两个抽象方法 `execute()` 和 `undo()`,分别用于执行命令和撤销命令。
```java
public abstract class Command {
public abstract void execute();
public abstract void undo();
}
```
接下来,我们可以定义具体的命令类,例如绘制矩形、椭圆和三角形的命令类。这些类都需要实现 `Command` 接口,同时需要持有一个接收者对象 `Receiver`,用于执行具体的绘制操作。
```java
public class DrawRectangleCommand extends Command {
private Receiver receiver;
public DrawRectangleCommand(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.drawRectangle();
}
@Override
public void undo() {
receiver.eraseRectangle();
}
}
public class DrawEllipseCommand extends Command {
private Receiver receiver;
public DrawEllipseCommand(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.drawEllipse();
}
@Override
public void undo() {
receiver.eraseEllipse();
}
}
public class DrawTriangleCommand extends Command {
private Receiver receiver;
public DrawTriangleCommand(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
receiver.drawTriangle();
}
@Override
public void undo() {
receiver.eraseTriangle();
}
}
```
我们还需要定义一个接收者类 `Receiver`,该类包含了具体的绘制操作。
```java
public class Receiver {
public void drawRectangle() {
System.out.println("绘制矩形");
}
public void eraseRectangle() {
System.out.println("擦除矩形");
}
public void drawEllipse() {
System.out.println("绘制椭圆");
}
public void eraseEllipse() {
System.out.println("擦除椭圆");
}
public void drawTriangle() {
System.out.println("绘制三角形");
}
public void eraseTriangle() {
System.out.println("擦除三角形");
}
}
```
最后,我们可以定义一个调用者类 `Invoker`,该类可以接受命令并执行或撤销命令。
```java
public class Invoker {
private List<Command> commandList = new ArrayList<>();
private int index = -1;
public void addCommand(Command command) {
commandList.add(command);
index++;
}
public void executeCommand() {
if (index >= 0 && index < commandList.size()) {
Command command = commandList.get(index);
command.execute();
}
}
public void undoCommand() {
if (index >= 0 && index < commandList.size()) {
Command command = commandList.get(index);
command.undo();
index--;
}
}
public void redoCommand() {
if (index >= -1 && index < commandList.size() - 1) {
index++;
Command command = commandList.get(index);
command.execute();
}
}
}
```
现在,我们可以使用以上类来实现一个图形绘制系统。例如,我们可以先创建一个 `Receiver` 对象,然后创建几个命令并传入该对象,最后将命令添加到 `Invoker` 中。
```java
public static void main(String[] args) {
Receiver receiver = new Receiver();
Command drawRectangleCommand = new DrawRectangleCommand(receiver);
Command drawEllipseCommand = new DrawEllipseCommand(receiver);
Command drawTriangleCommand = new DrawTriangleCommand(receiver);
Invoker invoker = new Invoker();
invoker.addCommand(drawRectangleCommand);
invoker.addCommand(drawEllipseCommand);
invoker.addCommand(drawTriangleCommand);
invoker.executeCommand();
invoker.undoCommand();
invoker.redoCommand();
}
```
上述代码中,我们首先创建了一个 `Receiver` 对象,然后创建了三个命令并传入该对象,最后将命令添加到 `Invoker` 中。我们执行了一次命令,然后撤销了该命令,最后重做了该命令。运行结果如下:
```
绘制矩形
擦除矩形
绘制矩形
```
类图如下:
![command-pattern](https://cdn.jsdelivr.net/gh/1071942338/picture_bed/blog_pictures/command-pattern.png)
阅读全文