Presentation is loading. Please wait.

Presentation is loading. Please wait.

1.

Similar presentations


Presentation on theme: "1."— Presentation transcript:

1 1

2 Liskov Substitution principle
The principle was introduced by Barbara Liskov in her conference keynote “Data abstraction” in A few years later, she published a paper with Jeanette Wing in which they defined the principle as: Let Φ(x) be a property provable about objects x of type T. Then Φ(y)should be true for objects y of type S where S is a subtype of T. The principle defines that objects of a superclass shall be replaceable with objects of its subclasses without breaking the application. That requires the objects of your subclasses to behave in the same way as the objects of your superclass.

3 The importance of LSP becomes obvious when you face the consequences of its violation.
If class has not been defined according to LSP, its necessary to go through the existing code and account for the special cases involving the delinquent subclasses, which breaks Open-Closed principle.

4 LSP compliance can be achieved by following a few rules, which are pretty similar to the Design By Contract concept defined by Bertrand Meyer. Pre-conditions cannot be strengthened. An overridden method of a subclass needs to accept the same input parameter values as the method of the superclass. Post-conditions cannot be weakened. Same rules that are applied for method output must be applied for overridden method. Invariants must be preserved. The most difficult and painful constraint to fulfill. Invariants are some time hidden in the base class and the only way to reveal them is to read the code of the base class.

5 The Example public class Bird { public void fly(){}
public class BirdTest{ public static void main(String[] args){     List<Bird> birdList = new ArrayList<Bird>();     birdList.add(new Bird());     birdList.add(new Crow());     birdList.add(new Ostrich());     letTheBirdsFly ( birdList );   }   static void letTheBirdsFly ( List<Bird> birdList ){    for ( Bird b : birdList ) {      b.fly(); } public class Bird { public void fly(){} public void eat(){} } public class Crow extends Bird {} public class Ostrich extends Bird{ public  void fly(){ throw new UnsupportedOperationException();   }

6 public class Bird { public void eat(){} } public class FlyingBird extends Bird { public void fly(){} public class Crow extends FlyingBird {} public class Ostrich extends Bird {} Model your classes based on behaviors not on properties; model your data based on properties and not on behaviors.

7 Summary It extends the Open/Closed principle and enables you to replace objects of a parent class with objects of a subclass without breaking the application. This requires all subclasses to behave in the same way as the parent class. To achieve that it’s necessary to follow these rules: Don’t implement any stricter validation rules on input parameters than implemented by the parent class. Apply at the least the same rules to all output parameters as applied by the parent class. No new exceptions should be thrown in derived class.

8 Thank you for your attention
Copyright: © 2018 Seavus. All rights reserved. | | |


Download ppt "1."

Similar presentations


Ads by Google