Command Pattern
- 명령들을 캡슐화(클래스)
- 명령을 수행 단위로 묶음
-> 명령을 추상화
- 특정 객체에 대한 특정 작업을 캡슐화 하여 요청을 하는 객체와 수행하는 객체를 분리한다.
- 요청을 객체화 형태로 캡슐화 하면 요청에 대한 저장 및 로깅, 연산의 취소를 지원할 수 있다.
- interface Shape {
- void draw(); // LSP(리스코프의 치환 법칙) : 자식은 부모로 대체 가능해야 한다.
- }
- class Rect implements Shape {
- @Override
- public void draw() {
- System.out.println("Draw Rect");
- }
- }
- class Circle implements Shape {
- @Override
- public void draw() {
- System.out.println("Draw Circle");
- }
- }
- interface ICommand {
- void execute();
- boolean canUndo();
- void undo();
- }
- abstract class AddCommand implements ICommand {
- private final List<Shape> shapes;
- public AddCommand(List<Shape> s) {
- this.shapes = s;
- }
- @Override
- public void execute() {
- shapes.add(createShape());
- }
- @Override
- public boolean canUndo() {
- return true;
- }
- @Override
- public void undo() {
- shapes.remove(shapes.size() - 1);
- }
- public abstract Shape createShape();
- }
- class AddRectCommand extends AddCommand {
- public AddRectCommand(List<Shape> s) {
- super(s);
- }
- @Override
- public Shape createShape() {
- return new Rect();
- }
- }
- class AddCircleCommand extends AddCommand {
- public AddCircleCommand(List<Shape> s) {
- super(s);
- }
- @Override
- public Shape createShape() {
- return new Circle();
- }
- }
- class DrawCommand implements ICommand {
- private final List<Shape> shapes;
- public DrawCommand(List<Shape> s) {
- shapes = s;
- }
- @Override
- public void execute() {
- for (Shape e : shapes) {
- e.draw();
- }
- }
- @Override
- public boolean canUndo() {
- return true;
- }
- @Override
- public void undo() {
- System.out.println("Screen Cleared!!!");
- }
- }
- class MacroCommand implements ICommand {
- // Composite Pattern
- private List<ICommand> commands = new ArrayList<>();
- public void addCommand(ICommand c) {
- commands.add(c);
- }
- // Ctrl + C / Ctrl + D
- // Ctrl + D
- @Override
- public void execute() {
- for (ICommand e : commands) {
- e.execute();
- }
- }
- @Override
- public boolean canUndo() {
- for (int i = 0; i < commands.size(); i++) {
- if (commands.get(i).canUndo() == false)
- return false;
- }
- return true;
- }
- @Override
- public void undo() {
- int n = commands.size() - 1;
- while (n >= 0) {
- commands.get(n).undo();
- n--;
- }
- }
- }
- public class Ex1 {
- private static Scanner scanner;
- public static void main(String[] args) {
- scanner = new Scanner(System.in);
- List<Shape> shapes = new ArrayList<>();
- Stack<ICommand> commandStack = new Stack<>();
- Stack<ICommand> redoStack = new Stack<>();
- MacroCommand mc = new MacroCommand();
- mc.addCommand(new AddRectCommand(shapes));
- mc.addCommand(new AddCircleCommand(shapes));
- mc.addCommand(new DrawCommand(shapes));
- mc.execute();
- commandStack.add(mc);
- ICommand command = null;
- while (true) {
- System.out.print("choice : ");
- int cmd = scanner.nextInt();
- switch (cmd) {
- case 1:
- command = new AddRectCommand(shapes);
- command.execute();
- commandStack.push(command);
- break;
- case 2:
- command = new AddCircleCommand(shapes);
- command.execute();
- commandStack.push(command);
- break;
- case 9:
- command = new DrawCommand(shapes);
- command.execute();
- commandStack.push(command);
- break;
- case -1:
- command = redoStack.pop();
- command.execute();
- case 0:
- // Undo(ctrl + z)
- command = commandStack.pop();
- if (command.canUndo()) {
- command.undo();
- redoStack.add(command);
- }
- break;
- }
- }
- }
- }
'java > design_pattern' 카테고리의 다른 글
Bridge Pattern - 브릿지 패턴 (0) | 2014.07.01 |
---|---|
Memento Pattern - 메멘토 패턴 (0) | 2014.07.01 |
Proxy Pattern - 대리자 패턴 (0) | 2014.07.01 |
Iterator Pattern - 열거자 (0) | 2014.07.01 |
Visitor Pattern - 방문자 패턴 (0) | 2014.07.01 |