Showing posts with label Design Principles. Show all posts
Showing posts with label Design Principles. Show all posts

Saturday, February 22, 2014

Open-Closed Principle (OCP)

@desc
  : Classes should be open for extension(확장), but closed for modification(코드 변경)

@example
  : Beverage(Super Class) & CondimentDecorator(Sub Class)
  : DarkRoast is Beverage, and Mocha, Whip are CondimentDecorator
  : Mocha는 Beverage(DarkRoast)의 Cost() 결과에 Mocha에 해당하는 처리만 덧붙인다.
  : Whip은 Beverage(DarkRoast + Mocha)의 Cost() 결과에 Whip에 해당하는 처리만 덧붙인다.
  : 즉 각각의 Decorator는 자신의 객체인 Beverage의 결과에는 아무런 영향을 주지 않으면서,
  : 동시에 필요한 기능을 확장하게 된다.




@reference
  : Head First Design Patterns
    by Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates, 1st Ed.

Strive for LOOSELY COUPLED designs between objects that interact

@desc
  : 서로 상호작용을 하는 객체 사이에서는 가능하면,
    느슨하게 결합하는 디자인을 사용해야 한다.

  : 느슨한 결합은 변경 사항이 생겨도 무난히 처리할 수 있는 유연성을 제공한다.
    객체 사이의 상호의존성을 최소화하기 때문이다.

@example
  : Subject Interface, Observer Interface



Ex1. Subject는 Observer에 대해 Observer interface를 구현한다는 것만 안다.
Observer가 무엇을 하는지, 어떻게 구현되는지는 알 필요가 없다.

Ex2. Observer는 언제든지 새로 추가할 수 있다.
Subject는 Observer interface 목록에만 의존한다.
각각의 Observer가 언제 추가되는지, 언제 제거되는지는 중요하지 않다.

Ex3. 새로운 형식의 Observer를 추가하려고 해도 Subject 변경은 없다.
새로운 형식의 Observer가 Observer interface만 구현한다면 문제 없다.

Ex4. Subject나 Observer가 바뀌더라도 서로에게 미치는 영향은 없다.
Subject interface, Observer interface를 구현한다는 조건만 만족하면 된다.
그 외 변경 사항은 서로에게 아무런 영향도, 문제도 주지 않는다.


@reference
  : Head First Design Patterns
    by Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates, 1st Ed.

Favor COMPOSITION over inheritance

@desc
  : 상속보다는 구성을 활용한다.
  : 구성을 이용하면 알고리즘군을 별로 클래스 집합으로 캡슐화 할 수 있다.
  : 구성을 이용하면 행동 인터페이스 구현을 통해 실행시에 행동을 바꿀 수 있다.

@example
  : Duck Class, Fly Behaviors

Ex1. Behavior Interface












Ex2. Setting Behavior Dynamically
Duck model = new ModelDuck();
model.performFly(); // 저는 못 날아요.

model.setFlyBehavior(new FlyRocketPowered());
model.performFly(); // 로켓 추진으로 날아갑니다!


@reference
  : Head First Design Patterns
    by Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates, 1st Ed.

Program to an INTERFACE, not an implementation

@desc
  : 구현이 아닌 인터페이스에 맞춰서 프로그래밍한다.

  : 바뀌는 부분은 따로 뽑아서 캡슐화시킨다. 
    그렇게 하면 나중에 바뀌지 않는 부분에는 영향을 미치지 않은 채로 
    그 부분만 고치거나 확장할 수 있다.

@example
  : Animal Class, Dog Class

Ex1. Programming to an implementation
Dog d = new Dog();
d.bark();

Ex2. Programming to an interface/supertype
Animal animal = new Dog();
animal.makeSound();

Ex3. Assign a concrete implementation object at runtime
Animal animal = getAnimal();
animal.makeSound();





@reference
  : Head First Design Patterns
    by Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates, 1st Ed.

TAKE the parts that VARY and ENCAPSULATE them

@desc
  : 애플리케이션에서 달라지는 부분을 찾아내고,
    달라지지 않는 부분으로부터 분리시킨다.

  : 바뀌는 부분은 따로 뽑아서 캡슐화시킨다.
    그렇게 하면 나중에 바뀌지 않는 부분에는 영향을 미치지 않은 채로
    그 부분만 고치거나 확장할 수 있다.

@example
  : Duck Class, FlyBehaviors & Quack Behaviors


@reference
  : Head First Design Patterns
    by Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates, 1st Ed.