Is this a violation of the Liskov Substitution Principle?

Posted by Paul T Davies on Programmers See other posts from Programmers or by Paul T Davies
Published on 2012-10-16T20:36:38Z Indexed on 2012/10/16 23:19 UTC
Read the original article Hit count: 184

Say we have a list of Task entities, and a ProjectTask sub type. Tasks can be closed at any time, except ProjectTasks which cannot be closed once they have a status of Started. The UI should ensure the option to close a started ProjectTask is never available, but some safeguards are present in the domain:

public class Task
{
     public Status Status { get; set; }

     public virtual void Close()
     {
         Status = Status.Closed;
     }
}

public ProjectTask : Task
{
     public override void Close()
     {
          if (Status == Status.Started) 
              throw new Exception("Cannot close a started Project Task");

          base.Close();
     }
}

Now when calling Close() on a Task, there is a chance the call will fail if it is a ProjectTask with the started status, when it wouldn't if it was a base Task. But this is the business requirements. It should fail. Can this be regarded as a violation?

© Programmers or respective owner

Related posts about design

Related posts about object-oriented-design