Am I violating LSP if the condition can be checked?
- by William
This base class for some shapes I have in my game looks like this. Some of the shapes can be resized, some of them can not.
private Shape shape;
public virtual void SetSizeOfShape(int x, int y)
{
if (CanResize()){
shape.Width = x;
shape.Height = y;
}
else {
throw new Exception("You cannot resize this shape");
}
}
public virtual bool CanResize()
{
return true;
}
In a sub class of a shape that I don't ever want to resize I am overriding the CanResize() method so a piece of client code can check before calling the SetSizeOfShape() method.
public override bool CanResize()
{
return false;
}
Here's how it might look in my client code:
public void DoSomething(Shape s)
{
if(s.CanResize()){
s.SetSizeOfShape(50, 70);
}
}
Is this violating LSP?