2 Code snippet wrong
source | link

Precondition is something that the calling code is responsible for, not the callee who actually should rely on the calling code to have met the preconditions. With regards to your code, the derived class imposed more restrictions for Dispose method to be called.

To be LSP compliant (and DbC, where the caller has to make sure preconditions apply), I would suggest a solution like this:

class Enemy

{

public virtual bool CanBeDestroyed()

{

return life<=0;

}

class Enemy

{

public virtual bool CanBeDestroyed()

{

  return life<=0;

}

    public void CheckHealth()

    {       assert.CanBeDestroyed(“true”); //precondition: CanBeDestroyed is true, caller’s responsibility to check before the method is called

            Dispose();

    }

} 



class Boss : Enemy

{

public override bool CanBeDestroyed()

{

  return life<=0 && PowerDestroyed;

}

}

}

class Boss : Enemy

{

public override bool CanBeDestroyed()

{

return life<=0 && PowerDestroyed;

}

}

Precondition is something that the calling code is responsible for, not the callee who actually should rely on the calling code to have met the preconditions. With regards to your code, the derived class imposed more restrictions for Dispose method to be called.

To be LSP compliant (and DbC, where the caller has to make sure preconditions apply), I would suggest a solution like this:

class Enemy

{

public virtual bool CanBeDestroyed()

{

return life<=0;

}

public void CheckHealth()

{       assert.CanBeDestroyed(“true”); //precondition: CanBeDestroyed is true, caller’s responsibility to check before the method is called

        Dispose();

}

}

class Boss : Enemy

{

public override bool CanBeDestroyed()

{

return life<=0 && PowerDestroyed;

}

}

Precondition is something that the calling code is responsible for, not the callee who actually should rely on the calling code to have met the preconditions. With regards to your code, the derived class imposed more restrictions for Dispose method to be called.

To be LSP compliant (and DbC, where the caller has to make sure preconditions apply), I would suggest a solution like this:

class Enemy

{

public virtual bool CanBeDestroyed()

{

  return life<=0;

}

    public void CheckHealth()

    {       assert.CanBeDestroyed(“true”); //precondition: CanBeDestroyed is true, caller’s responsibility to check before the method is called

            Dispose();

    }

} 



class Boss : Enemy

{

public override bool CanBeDestroyed()

{

  return life<=0 && PowerDestroyed;

}

}
1
source | link

Precondition is something that the calling code is responsible for, not the callee who actually should rely on the calling code to have met the preconditions. With regards to your code, the derived class imposed more restrictions for Dispose method to be called.

To be LSP compliant (and DbC, where the caller has to make sure preconditions apply), I would suggest a solution like this:

class Enemy

{

public virtual bool CanBeDestroyed()

{

return life<=0;

}

public void CheckHealth()

{       assert.CanBeDestroyed(“true”); //precondition: CanBeDestroyed is true, caller’s responsibility to check before the method is called

        Dispose();

}

}

class Boss : Enemy

{

public override bool CanBeDestroyed()

{

return life<=0 && PowerDestroyed;

}

}