Skip to content

Commit 6e5bdca

Browse files
committed
add Command pattern v4 implementation
1 parent 7710f32 commit 6e5bdca

File tree

8 files changed

+161
-0
lines changed

8 files changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.command.v4;
2+
3+
public class Application {
4+
5+
public static void main(String[] args) {
6+
7+
var videoEditor = new VideoEditor();
8+
var history = new History();
9+
10+
System.out.println("Initial State: " + videoEditor);
11+
12+
var setTextConcreteCommand = new SetTextConcreteCommand("Video Title", videoEditor, history);
13+
setTextConcreteCommand.execute();
14+
System.out.println("TEXT: " + videoEditor);
15+
16+
var setContrast = new SetContrastConcreteCommand(1, videoEditor, history);
17+
setContrast.execute();
18+
System.out.println("CONTRAST: " + videoEditor);
19+
20+
var undoConcreteCommand = new UndoConcreteCommand(history);
21+
undoConcreteCommand.execute();
22+
System.out.println("UNDO: " + videoEditor);
23+
24+
undoConcreteCommand.execute();
25+
System.out.println("UNDO: " + videoEditor);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.command.v4;
2+
3+
public interface Command {
4+
5+
void execute();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.command.v4;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class History {
7+
private Deque<UndoableCommand> undoableCommandDeque = new ArrayDeque<>();
8+
9+
public void push(UndoableCommand undoableCommand) {
10+
undoableCommandDeque.push(undoableCommand);
11+
}
12+
13+
public UndoableCommand pop() {
14+
return undoableCommandDeque.pop();
15+
}
16+
17+
public int size() {
18+
return undoableCommandDeque.size();
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.command.v4;
2+
3+
public class SetContrastConcreteCommand implements UndoableCommand {
4+
5+
private float contrast;
6+
private float previousContrast;
7+
private VideoEditor videoEditor;
8+
private History history;
9+
10+
public SetContrastConcreteCommand(float contrast, VideoEditor videoEditor, History history) {
11+
this.contrast = contrast;
12+
this.videoEditor = videoEditor;
13+
this.history = history;
14+
}
15+
16+
@Override
17+
public void execute() {
18+
previousContrast = videoEditor.getContrast();
19+
videoEditor.setContrast(contrast);
20+
history.push(this);
21+
}
22+
23+
@Override
24+
public void unexecute() {
25+
videoEditor.setContrast(previousContrast);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.command.v4;
2+
3+
public class SetTextConcreteCommand implements UndoableCommand {
4+
5+
private String text;
6+
private String previousText;
7+
private VideoEditor videoEditor;
8+
private History history;
9+
10+
public SetTextConcreteCommand(String text, VideoEditor videoEditor, History history) {
11+
this.text = text;
12+
this.videoEditor = videoEditor;
13+
this.history = history;
14+
}
15+
16+
@Override
17+
public void execute() {
18+
previousText = videoEditor.getText();
19+
videoEditor.setText(text);
20+
history.push(this);
21+
}
22+
23+
@Override
24+
public void unexecute() {
25+
videoEditor.setText(previousText);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.command.v4;
2+
3+
public class UndoConcreteCommand implements Command {
4+
5+
private History history;
6+
7+
public UndoConcreteCommand(History history) {
8+
this.history = history;
9+
}
10+
11+
@Override
12+
public void execute() {
13+
if (history.size() > 0) {
14+
history.pop().unexecute();
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.command.v4;
2+
3+
public interface UndoableCommand extends Command {
4+
5+
void unexecute();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.command.v4;
2+
3+
public class VideoEditor {
4+
5+
private float contrast;
6+
private String text;
7+
8+
public float getContrast() {
9+
return contrast;
10+
}
11+
12+
public void setContrast(float contrast) {
13+
this.contrast = contrast;
14+
}
15+
16+
public String getText() {
17+
return text;
18+
}
19+
20+
public void setText(String text) {
21+
this.text = text;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "VideoEditor{" +
27+
"contrast=" + contrast +
28+
", text='" + text + '\'' +
29+
'}';
30+
}
31+
}

0 commit comments

Comments
 (0)