SOLID

객체 지향 설계 원칙

SOLID (SRP, OCP, LSP, ISP, DIP)

1. Single Responsibility Principle

A class should only have a single responsibility.
하나의 클래스는 하나의 책임을 가진다.



2. Open-Closed Principle

Software entities should be open for extension, but closed for modification.
소프트웨어 요소는 확장에 개방적이고, 변경에 폐쇄적이어야 한다.



3. Liskov Substitution Principle

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.
서브 타입은 언제나 기반 타입으로 치환 가능해야 한다.

대표 예제 
- Rectangle 클래스를 상속한 Square의 클래스는 기반 타입의 getArea() 동작을 보장하지 못함.
class Rectangle {
private int width;
private int height;

public void setHeight(int height) {
this.height = height;
}

public int getHeight() {
return this.height;
}

public void setWidth(int width) {
this.width = width;
}

public int getWidth() {
return this.width;
}

public int area() {
return this.width * this.height;
}
}

class Square extends Rectangle {
@Override
public void setHeight(int value) {
this.width = value;
this.height = value;
}

@Override
public void setWidth(int value) {
this.width = value;
this.height = value;
}
}


4. Interface Segregation Principle

Many client-specific interfaces are better than on general-purpose interface
구현하지 않는 인터페이스가 없도록 한다. 대신 여러개의 interface 들을 여러게 만드는 것이 낫다.



5. Dependency Inversion Principle

Depend upon abstractions, not concretions.
추상화에 의존해야 한다.


댓글