Command Design [커맨드 디자인]

  1. Design Object [디자인목적]

    1. 프로그램에는 다양한 명령어가 존재하며, 각 명령어를 보통 기능(Method)화 되어 관리
    2. 바로 실행한 명령을 되돌릴 때, queue를 사용해야 하며, queue를 사용하기 위해 명령어를 객체화
    3. 객체화 시킨 명령어들은 확장이 용이.<신규 명령어 생성 / 명령어 기능 수정 >
  2. Options [대안들]

    1. 팩토리 패턴
  3. Benefit [디자인 장점]

    1. 여러 객체간 공통부분을 묶음으로써, 대량 객체 생성 시에도 공간을 절약할 수 있다.

    2. 공간을 절약함으로써, application의 성능 향상

    3. 한번 생성 된 객체는 해제되지 않고, 풀(pool)에서 관리

    4. 게임 등에서 케릭터 구현 시 사

  4. Structure & Sample Code [구조와 샘플코드]

    1) 글자를 복사하는 기능.

    1. UML

    2. 구조

      -> Letter 객체 : 한 글자와 getter를 포함
      -> LetterFactory : Letter를 담는 HashMap을 유지하며, 새로운 key가 생성되면, Letter를 생성하고, 이미 존재하면, HashMap에 존재하는 Letter객체 재사용.
      -> WordProcessor : 입력받은 String 의 Letter를 arrayList로 보관하며, 출력함.
      -> client에서 복사할 문자열을 wordprocessor로 복사하고, LetterFactory를 통해 객체 재사용 및 생성.

    1)App(client)

public class Client {

    public static void main(String[] args) {
        WordProcessor processor = new WordProcessor();

        String textToAdd = "Hello i'm aaaa wwworddd pprocessorrrr";  //source string
        int length = textToAdd.length();


        LetterFactory factory = new LetterFactory();   //letter factory

        for(int i = 0; i<length;i++){

            String value = textToAdd.substring(i,i+1);
            /*
             * factory will check the key string value is exists in memory
             */
            processor.addLetter(factory.createLetter(value));   

        }

        processor.printLetters();
    }


}

2) WordProcessor

public class WordProcessor {

    //array for contain copied string value letter instance
    private List<Letter> letters = new ArrayList<Letter>();   

    public void addLetter(Letter letter){
        this.letters.add(letter);
    }

    public void printLetters(){
        for (Letter letter : letters) {
            System.out.print(letter.getValue());
        }
    }

}

3) LetterFactory

public class LetterFactory {

    //Map object to contain generated Letter object 
    private Map<String, Letter> letterMap = new HashMap<String, Letter>();

    /**
     * if Letter has key value exists return that Letter object
     * else create new Letter object and put it into Map
    */
    public Letter createLetter(String key){
        Letter letter = letterMap.get(key);
        if(letter == null){
            letter = new Letter(key);
            letterMap.put(key,letter);
        }

        return letterMap.get(key);
    }

}

4) Letter

public class Letter {

    private String value;

    public Letter(String value) {
        System.out.println("New letter created with value: "+value);
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

results matching ""

    No results matching ""