Skip to content

Instantly share code, notes, and snippets.

@DarkPurpleKnight
Created December 28, 2021 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarkPurpleKnight/e88505d1317dcbcb50836a25ea7b153d to your computer and use it in GitHub Desktop.
Save DarkPurpleKnight/e88505d1317dcbcb50836a25ea7b153d to your computer and use it in GitHub Desktop.
Code with Mosh - The Ultimate Design Patterns: Part 1 - Command Pattern - Exercise
package com.codewithmosh.command;
public interface Command {
void execute();
}
package com.codewithmosh.command;
public class ContrastCommand implements UndoableCommand {
private float previousContrast;
private float newContrast;
private VideoEditor editor;
private History history;
public ContrastCommand(float newContrast, VideoEditor editor, History history) {
this.newContrast = newContrast;
this.editor = editor;
this.history = history;
}
@Override
public void execute() {
previousContrast = editor.getContrast();
editor.setContrast(newContrast);
history.push(this);
}
@Override
public void unexecute() {
editor.setContrast(previousContrast);
}
}
package com.codewithmosh.command;
import java.util.ArrayDeque;
import java.util.Deque;
public class History {
private Deque<UndoableCommand> commands = new ArrayDeque<>();
public void push(UndoableCommand command) {
// commands.add(command);
commands.addFirst(command);
}
public UndoableCommand pop() {
return commands.pop();
}
public boolean isEmpty() {
return commands.isEmpty();
}
}
package com.codewithmosh;
import com.codewithmosh.command.*;
public class Main {
public static void main(String[] args) {
var history = new History();
var editor = new VideoEditor();
System.out.println(editor.toString());
var textCommand = new TextCommand("Hello World", editor, history);
textCommand.execute();
System.out.println(editor.toString());
var contrastCommand = new ContrastCommand(0.8f, editor, history);
contrastCommand.execute();
System.out.println(editor.toString());
var contrastCommand2 = new ContrastCommand(1.0f, editor, history);
contrastCommand2.execute();
System.out.println(editor.toString());
var undoCommand = new UndoCommand(history);
undoCommand.execute();
System.out.println(editor.toString());
undoCommand.execute();
System.out.println(editor.toString());
}
}
package com.codewithmosh.command;
public class TextCommand implements UndoableCommand {
private String previousContent;
private String newText;
private VideoEditor editor;
private History history;
public TextCommand(String newText, VideoEditor editor, History history) {
this.newText = newText;
this.editor = editor;
this.history = history;
}
@Override
public void execute() {
previousContent = editor.getText();
editor.setText(newText);
history.push(this);
}
@Override
public void unexecute() {
editor.setText(previousContent);
}
}
package com.codewithmosh.command;
public interface UndoableCommand extends Command {
void unexecute();
}
package com.codewithmosh.command;
public class UndoCommand implements Command {
private History history;
public UndoCommand(History history) {
this.history = history;
}
@Override
public void execute() {
if (!history.isEmpty()) {
history.pop().unexecute();
}
}
}
package com.codewithmosh.command;
public class VideoEditor {
private float contrast = 0.5f;
public String getText() {
return text;
}
private String text;
public void setText(String text) {
this.text = text;
}
public void removeText() {
this.text = "";
}
public float getContrast() {
return contrast;
}
public void setContrast(float contrast) {
this.contrast = contrast;
}
@Override
public String toString() {
return "VideoEditor{" +
"contrast=" + contrast +
", text='" + text + '\'' +
'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment