FooInterface다음과 같은 서명을 가진 인터페이스 가 있다고 가정 해 봅시다 .
interface FooInterface {
public function doSomething(SomethingInterface something);
}
그리고 ConcreteFoo그 인터페이스를 구현 하는 구체적인 클래스 :
class ConcreteFoo implements FooInterface {
public function doSomething(SomethingInterface something) {
}
}
ConcreteFoo::doSomething()특별한 유형의 SomethingInterface객체 가 전달되면 고유 한 작업을 수행 하고 싶습니다 (라고합니다 SpecialSomething).
메서드의 전제 조건을 강화하거나 새로운 예외를 던지면 확실히 LSP 위반이지만 SpecialSomething일반 SomethingInterface객체에 폴백을 제공하면서 특수한 경우에 LSP 위반이 계속 발생 합니까? 다음과 같은 것 :
class ConcreteFoo implements FooInterface {
public function doSomething(SomethingInterface something) {
if (something instanceof SpecialSomething) {
// Do SpecialSomething magic
}
else {
// Do generic SomethingInterface magic
}
}
}
doSomething()메소드 의 목적은 타입 변환이다SpecialSomething: 만약 그것이 수신되면SpecialSomething수정되지 않은 객체를 반환하는 반면, 일반SomethingInterface객체를 수신 하면 알고리즘을 실행하여 객체로 변환한다SpecialSomething. 전제 조건과 사후 조건이 동일하게 유지되므로 계약을 위반 한 것으로 생각하지 않습니다.