SOLID

1 minute read

SOLID

Single Responsibility Principle(SRP)

  • A class should have one, and only one reason to change
  • 인터페이스는 단 하나의 퍼블릭 인터페이스를 가져야하고 나머지는 서포트한다로 이해하면 될까>

Open Closed Principle(OCP)

  • Software entities should be open for extension but closed for modification
  • 클라이언트에서 case문 추가 else문 추가ㄴㄴ
  • 재사용이랑 기능 재사용이 아니라 클라이언트 코드의 재사용을 의미한다
  • Use TDD and listen to the tests. 한번은 속아주고 리팩토링하자

Liskov Substitution Principle(LSP)

  • Subtypes must be substitutable for their base types
  • Check when you decide to use inheritance or not(is-a)

The owner of f might want to put test code for Ctype
void f(PType x) {
if (x instanceof( Ctype)) throw new RuntimeException();
// some code misbevaves when x is Ctype
… }
The above reaction is worse. Now, it is violating OCP, too!  f is not closed to all various subtypes of PType

-> OCP까지 어길 가능성이 높다!

Dependency Inversion Principle(DIP)

  • High-level modules should not depend on low-level modules. Both should depend on abstractions
  • Why Inversion?
     DIP attempts to “invert” the dependencies that result from a structured analysis and design approach

레이어드 시스템에서의 dip
(이승진 교수님 자료 초반에 있음)
디아이피에서 역전이라는 말은 내가 필요한 것을 받아서 쓴다는 이야기이다.
일반적으로는 내가 필요한것을 내가 구현할테니까

Interface Segregation Principle(ISP)

  • 인터페이스 분리 원칙
  • Client should not be forced to depend on methods they do not use
  • 자기가 사용하지 않는 인터페이스 때문에 의존관계를 가져서는 안된다 -> 결국 인터페이스를 더 잘게 나눠서 이런 일이 없도록 하라(MORE COHESIVE)

bad

better