아무거나

[Design Pattern] Command Pattern 본문

Java/Java

[Design Pattern] Command Pattern

전봉근 2020. 1. 12. 13:47
반응형

커맨드 패턴(Command Pattern)

요청을 객체의 형태로 캡슐화하여 사용자가 보낸 요청을 나중에 이용할 수 있도록 매서드 이름, 매개변수 등 요청에 필요한 정보를 저장 또는 로깅, 취소할 수 있게 하는 패턴이다.

 

 

Command 인터페이슬르 구현하면서 사용할 수 있다.

커맨드 패턴 예시 - 1

Command 인터페이스를 구현한다.
[Command.java]

package com.bkjeon.command;

public interface Command {

    void execute();

}

메인 클래스를 구현한다.
[Main.java]

package com.bkjeon.command;

import java.util.LinkedList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<Command> list = new LinkedList<>();
        list.add(new Command() {
            @Override
            public void execute() {
                System.out.println("작업 1");
            }
        });
        list.add(new Command() {
            @Override
            public void execute() {
                System.out.println("작업 2");
            }
        });
        list.add(new Command() {
            @Override
            public void execute() {
                System.out.println("작업 3");
            }
        });
    }

}

실행

작업 1
작업 2
작업 3

커맨드 패턴 예시 - 2

StringPrintCommand 생성
[StringPrintCommand.java]

package com.bkjeon.command;

public class StringPrintCommand implements Command {

    protected String string;

    public StringPrintCommand(String string) {
        this.string = string;
    }

    @Override
    public void execute() {
        System.out.println(this.string);
    }

    @Override
    public int compareTo(Command command) {
        StringPrintCommand other = (StringPrintCommand) command;
        return this.string.length() - other.string.length();
    }

}

Main 클래스 수정
[Main.java]

package com.bkjeon.command;

import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;

public class Main {

    public static void main(String[] args) {
        /*
        List<Command> list = new LinkedList<>();
        list.add(new Command() {
            @Override
            public void execute() {
                System.out.println("작업 1");
            }
        });
        list.add(new Command() {
            @Override
            public void execute() {
                System.out.println("작업 2");
            }
        });
        list.add(new Command() {
            @Override
            public void execute() {
                System.out.println("작업 3");
            }
        });
         */

        PriorityQueue<Command> queue = new PriorityQueue<>();
        queue.add(new StringPrintCommand("A"));
        queue.add(new StringPrintCommand("AB"));
        queue.add(new StringPrintCommand("ABC"));
        queue.add(new StringPrintCommand("ABCD"));

        for (Command command: queue) {
            command.execute();
        }
    }

}

실행결과

A
AB
ABC
ABCD

결론적으로 커맨드 패턴은 명령이 객체화 되었다는게 핵심이다.

 

참고: https://www.inflearn.com/course/%EC%9E%90%EB%B0%94-%EB%94%94%EC%9E%90%EC%9D%B8-%ED%8C%A8%ED%84%B4/dashboard

반응형

'Java > Java' 카테고리의 다른 글

Stream 정렬 관련 Example  (0) 2020.05.28
조합(=combination) 구하기  (0) 2020.05.16
[Design Pattern] Proxy Pattern  (0) 2020.01.12
[Design Pattern] Flyweight Pattern  (0) 2020.01.12
[Design Pattern] Memento Pattern  (0) 2020.01.12
Comments