Created
December 28, 2021 21:21
Code with Mosh - The Ultimate Design Patterns: Part 1 - Command Pattern - Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codewithmosh.command; | |
public interface Command { | |
void execute(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codewithmosh.command; | |
public interface UndoableCommand extends Command { | |
void unexecute(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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