jeudi 5 janvier 2017

Liskov substitution design principle adaptation

Let's say I have an abstract class bird, and one of its functions is fly(int height).

I have numerous different bird classes, each with it's own different implementation of fly, and the function is used extensively across the application.

One day my boss comes and demands I add a duck, which does everything the other birds do except that it doesn't fly but rather swims in the application's pond.

Adding the duck as sub-type of bird violates Liskov substitution rule because when calling duck.fly we would either throw an exception, do nothing, or violate the correctness principle.

How would you go about introducing this change while keeping in mind the SOLID design principles?

Aucun commentaire:

Enregistrer un commentaire