Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Command 패턴(김동석)

Command Pattern : Real-World Analogy (출처:https://refactoring.guru/design-patterns/command)

해결하려는 문제

  1. view(presentation)과 비즈니스 로직의 직접 연관을 제거(행위가 소스에 하드코딩되어 있다면 런타임에 추가/변경이 불가능)

용도/목적

  1. 조건문이 많은 복잡한 로직을 간단하게
  2. invoker와 receiver 사이에 command를 두어 직접 연관을 제거하여 새로운 command가 추가되더라도 invoker를 수정할 필요가 없음

특징

  1. 행위behavioral 패턴
  2. 눈에 보이지 않는 무형의 개념(행위)도 객체화 할 수 있다.

고려사항

  1. command용 interface(TextFileOperation.java)는 @FunctionalInterface로 할 수 있다.

클래스 다이어그램

Command Method 패턴(김동석)

소스

  1. client : command를 초기화하고 invoker를 호출
    • TextFileOperationExecutorWithoutCommandTest.java : command를 사용하지 않은 샘플 테스트용
    • TextFileOperationExecutorWithCommandTest.java : command를 사용한 샘플 테스트용
  2. invoker : command를 받아 interface에 정의된 메소드를 호출하고 필요 시 이력 저장용 list에 저장
    • TextFileOperationExecutorWithCommand.java
  3. receiver : 실제 로직이 정의된 클래스
    • TextFile.java
  4. command : command 객체
    • TextFileOperation.java : command용 interface
    • OpenTextFileOperation.java : 파일 open하기 command
    • SaveTextFileOperation.java : 파일 save하기 command

참고