OO Design Principles (PPT 15) Flashcards
What are the three design principles to be aware of?
- Open-closed principle
- Dependency-Inversion principle
- Liskov-substitution principle
What is the Open-Closed principle?
Classes should be open for extension but closed to modification. Meaning you should be able to extend the behaviour of a class without modifying the already working code
How do you implement the open closed principle?
Implement Inheritance
What is the Dependency-Inversion Principle?
High level modules should not depend on low level modules, both should depend on an abstraction.
How to you solve the Dependency-Inversion Principle?
You define an abstraction of the behaviour. Each device must derive itself from this behaviour
What is the Liskov Substitution Principle?
Child classes should be substitutable for their parents
How do we prevent the Liskov Substitution Principle?
- Design by contract (define pre and post conditions on operations)
- Post conditions should be at least as strong as the parent class
- Also use pre conditions