부록. Class Diagram Code 변환 실전 :: 소림사의 홍반장!

부록. Class Diagram Code 변환 실전

2019.02.19 22:20 - 정열의 삘쏘굿

이제 실전이다!


실제 패턴이 적용된 다음 클래스 다이어그램을 보고 각 클래스들의 코드들을 생각해보자.


그리고 나서, 생각한 코드와 어느 정도 일치하는지 코드를 확인하여 보자.




design pattern model 해석 실전

design pattern
diagram
code
Adapter

App 원본 접기
public class App {
  public static void main(String[] args) {
    Captain captain = new Captain(new BattleFishingBoat());
    captain.move();
    captain.fire();
  }
}
BattleFishingBoat 원본 접기
public class BattleFishingBoat implements BattleShip {
 
  private static final Logger LOGGER = LoggerFactory.getLogger(BattleFishingBoat.class);
 
  private FishingBoat boat;
 
  public BattleFishingBoat() {
    boat = new FishingBoat();
  }
 
  @Override
  public void fire() {
    LOGGER.info("fire!");
  }
 
  @Override
  public void move() {
    boat.sail();
  }
}
BattleShip 원본 접기
public interface BattleShip {
 
  void fire();
 
  void move();
 
}
Captain 원본 접기
public class Captain implements BattleShip {
 
  private BattleShip battleship;
 
  public Captain() {
 
  }
 
  public Captain(BattleShip battleship) {
    this.battleship = battleship;
  }
 
  public void setBattleship(BattleShip battleship) {
    this.battleship = battleship;
  }
 
  @Override
  public void fire() {
    battleship.fire();
  }
 
  @Override
  public void move() {
    battleship.move();
  }
 
}
FishingBoat 원본 접기
public class FishingBoat {
 
  private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
 
  public void sail() {
    LOGGER.info("The Boat is moving to that place");
  }
 
  public void fish() {
    LOGGER.info("fishing ...");
  }
 
}
Composite

App 원본 접기
public class App {
 
  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
  public static void main(String[] args) {
    LOGGER.info("Message from the orcs: ");
 
    LetterComposite orcMessage = new Messenger().messageFromOrcs();
    orcMessage.print();
 
    LOGGER.info("\n");
 
    LOGGER.info("Message from the elves: ");
 
    LetterComposite elfMessage = new Messenger().messageFromElves();
    elfMessage.print();
  }
}
Letter 원본 접기
public class Letter extends LetterComposite {
 
  private char c;
 
  public Letter(char c) {
    this.c = c;
  }
 
  @Override
  protected void printThisBefore() {
    System.out.print(c);
  }
 
  @Override
  protected void printThisAfter() {
    // nop
  }
}
LetterComposite 원본 접기
public abstract class LetterComposite {
 
  private List<LetterComposite> children = new ArrayList<>();
 
  public void add(LetterComposite letter) {
    children.add(letter);
  }
 
  public int count() {
    return children.size();
  }
 
  protected abstract void printThisBefore();
 
  protected abstract void printThisAfter();
 
  /**
   * Print
   */
  public void print() {
    printThisBefore();
    for (LetterComposite letter : children) {
      letter.print();
    }
    printThisAfter();
  }
}
Messenger 원본 접기
public class Messenger {
 
  LetterComposite messageFromOrcs() {
 
    List<Word> words = new ArrayList<>();
 
    words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), new Letter('e'), new Letter(
        'r'), new Letter('e'))));
    words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
        'r'), new Letter('e'))));
    words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
    words.add(new Word(Arrays.asList(new Letter('a'))));
    words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'), new Letter('i'), new Letter(
        'p'))));
    words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
        'r'), new Letter('e'))));
    words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
    words.add(new Word(Arrays.asList(new Letter('a'))));
    words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'), new Letter('y'))));
 
    return new Sentence(words);
 
  }
 
  LetterComposite messageFromElves() {
 
    List<Word> words = new ArrayList<>();
 
    words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'), new Letter('c'), new Letter(
        'h'))));
    words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'), new Letter('n'), new Letter(
        'd'))));
    words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'), new Letter('u'), new Letter(
        'r'), new Letter('s'))));
    words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'), new Letter('o'), new Letter(
        'm'))));
    words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'), new Letter('u'), new Letter(
        'r'))));
    words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'), new Letter('u'), new Letter(
        't'), new Letter('h'))));
 
    return new Sentence(words);
 
  }
 
}
Sentence 원본 접기
public class Sentence extends LetterComposite {
 
  /**
   * Constructor
   */
  public Sentence(List<Word> words) {
    for (Word w : words) {
      this.add(w);
    }
  }
 
  @Override
  protected void printThisBefore() {
    // nop
  }
 
  @Override
  protected void printThisAfter() {
    System.out.print(".");
  }
}
Word 원본 접기
public class Word extends LetterComposite {
 
  /**
   * Constructor
   */
  public Word(List<Letter> letters) {
    for (Letter l : letters) {
      this.add(l);
    }
  }
 
  @Override
  protected void printThisBefore() {
    System.out.print(" ");
  }
 
  @Override
  protected void printThisAfter() {
    // nop
  }
}

Decorator

App 원본 접기
public class App {
 
  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
 
  public static void main(String[] args) {
 
    // simple troll
    LOGGER.info("A simple looking troll approaches.");
    Troll troll = new SimpleTroll();
    troll.attack();
    troll.fleeBattle();
    LOGGER.info("Simple troll power {}.\n", troll.getAttackPower());
 
    // change the behavior of the simple troll by adding a decorator
    LOGGER.info("A troll with huge club surprises you.");
    Troll clubbed = new ClubbedTroll(troll);
    clubbed.attack();
    clubbed.fleeBattle();
    LOGGER.info("Clubbed troll power {}.\n", clubbed.getAttackPower());
  }
}
ClubbedTroll 원본 접기
public class ClubbedTroll extends TrollDecorator {
 
  private static final Logger LOGGER = LoggerFactory.getLogger(ClubbedTroll.class);
 
  public ClubbedTroll(Troll decorated) {
    super(decorated);
  }
 
  @Override
  public void attack() {
    super.attack();
    LOGGER.info("The troll swings at you with a club!");
  }
 
  @Override
  public int getAttackPower() {
    return super.getAttackPower() + 10;
  }
}
SimpleTroll 원본 접기
public class SimpleTroll implements Troll {
 
  private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTroll.class);
 
  @Override
  public void attack() {
    LOGGER.info("The troll tries to grab you!");
  }
 
  @Override
  public int getAttackPower() {
    return 10;
  }
 
  @Override
  public void fleeBattle() {
    LOGGER.info("The troll shrieks in horror and runs away!");
  }
}
Troll 원본 접기
public interface Troll {
 
  void attack();
 
  int getAttackPower();
 
  void fleeBattle();
 
}
TrollDecorator 원본 접기
public class TrollDecorator implements Troll {
 
  private Troll decorated;
 
  public TrollDecorator(Troll decorated) {
    this.decorated = decorated;
  }
 
  @Override
  public void attack() {
    decorated.attack();
  }
 
  @Override
  public int getAttackPower() {
    return decorated.getAttackPower();
  }
 
  @Override
  public void fleeBattle() {
    decorated.fleeBattle();
  }
}













같이 보기






References...


"Unified Modeling Language". wikipedia.org. 2019-01-29.

"UML Class Diagrams Reference". uml-diaframs.org. 2019-01-29.

"UML". namu.wiki. 2019-01-29.

"JAVA 객체지향 디자인 패턴", 한빛미디어

"[UML] Class Diagram 클래스 다이어그램". tistory.com. 2019-01-30.

"UML: 클래스 다이어그램과 소스코드 매핑". nextree.co.kr. 2019-01-30.






다른 카테고리의 글 목록

Dev. 객체지향개발/디자인패턴 카테고리의 포스트를 톺아봅니다