나는 올바른 방법으로 OOP와 OOD 원칙을 배우려고 노력하고 있습니다. Liskov 치환 원칙과 PRE 및 POST 조건에 대한 설명을 듣고 싶습니다. 일부 주제는 여기에서 읽었으며 일부 기사는 http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod 및 기타 장소에서 읽었습니다.고체 원칙 : LSP 위반
나는 매우 그들에 대한 사전 및 사후 조건에 내 가정 간단한 기본 클래스와 몇 가지 예를 들어 서브 클래스를 작성하고 나는 그들이 올바른 있는지 알고 싶습니다. 댓글 줄은 내가 생각하기에 : PRE 및 POST 조건을 위반하는지 여부입니다.
public abstract class BaseClass
{
public virtual int GetResult(int x, int y)
{
if (x > 10 && y < 20)
{
return y - x;
}
throw new Exception();
}
}
public class LSPExample1 : BaseClass
{
public override int GetResult(int x, int y)
{
// PRE: weakened pre condition is ok
if (x > 10 && y <= 15)
{
// POST: Is it ok? because the available result range is narrowed by y <= 15
return y - x;
}
throw new Exception();
}
}
public class LSPExample2 : BaseClass
{
public override int GetResult(int x, int y)
{
// PRE: Same as base - OK
if (x > 10 && y < 20)
{
// POST: I assume it's bad because of parameters place changed in substraction ?
return x-y;
}
throw new Exception();
}
}
public class LSPExample3 : BaseClass
{
public override int GetResult(int x, int y)
{
// PRE Condition is bad (Strenghtened) because of (X >5) and (Y>20) ?
if (x > 5 && y > 20)
{
// POST condition is ok because base class do substraction which is weaker than multiplication ?
return x * y;
}
throw new Exception();
}
}
정말 시간
요점. 좋은거야. – niksofteng