Am I violating LSP if the condition can be checked?
Posted
by
William
on Programmers
See other posts from Programmers
or by William
Published on 2014-08-22T16:42:01Z
Indexed on
2014/08/22
22:32 UTC
Read the original article
Hit count: 224
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?
© Programmers or respective owner