카테고리 없음
SOLID - OOP design principle[4] - ISP
Jun.LEE
2024. 3. 11. 20:39
In the field of software engineering, the interface segregation principle (ISP) states that no code should be forced to depend on methods it does not use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them
위키피디아에서는 위와 같이 설명하고 있다. 간단하게 말하면
Interface를 너무 큰 범위로 잡아서 구현부에서 사용하지 않는 메서드를 만들지 말라는 이야기이다.
예시를 통해서 알아보자.
interface Car {
fun heat()
fun cool()
fun drive()
}
차는 운전을 할 수 있고 실내 온도조절이 가능하다.
이때 공기온도조절장치 클래스가 필요한 상황이라고 가정하자.
class AirConditional: Car {
override fun heat() {}
override fun cool() {}
override fun drive() {}
}
위 처럼 AirConditional 클래스를 만들어 줄 수 있다.
하지만 위 클래스의 drive method는 사용되지 않을 것이다.
이는 ISP에 위배되는 설계이다.
따라서, 위 상황이라면 인터페이스의 기능을 더 작게 나누어 주는 것이 좋다.
interface Vehicle {
fun drive()
}
interface AirConditional {
fun heat()
fun cool()
}
class Car: Vehicle, AirConditional {
override fun drive() {
TODO("Not yet implemented")
}
override fun heat() {
TODO("Not yet implemented")
}
override fun cool() {
TODO("Not yet implemented")
}
}