Lecture 4-Software design principles Flashcards
What is the solution to code reuse?
Design patterns : middle between reuse of classes at the lowest level, and reuse of frameworks at the highest level *(Hollywood principle)
What is encapsulation?
Separating the code that varies from the code that stays the same, to minimize the effects caused by the changes.
What are the 4 types of encapsulation?
-Encapsulation on method level–>(if a method does 2 things, separate them)
-Encapsulation on class level–> (create a helper class)
-Program to an interface, and not an implementation –>(make classes dependent on interface and not on concrete classes)
-Favour composition (has-a relationship) over inheritance (is-a relationship) → we can replace behaviour at runtime (Strategy pattern)
What is the open/closed principle?
Classes should be open for extension, but closed for modification :
-Instead, we can create subclass and override parts of the original class that we want to behave differently.
- BUT If you know that there’s a bug in the class, just go on and fix it; don’t create a subclass for it.
What is the Liskov Subsitution principle (LSP)?
Derived classes should extend without replacing the functionality of old classes.
AKA : when overriding, extend the base behaviour instead of fully replacing it.
What are the 6 principles of LSP? #1
-Parameter types in a method of subclass should match or be more abstract than parameter types in the method of the superclass.
What are the 6 principles of LSP? #2
The return type in a method of a subclass should match or be a subtype of the return type in the method of the superclass.
What are the 6 principles of LSP? #3
A method in a subclass shouldn’t throw types of exceptions that the base method isn’t expected to throw.
What are the 6 principles of LSP? #4
-A subclass shouldn’t strengthen pre-conditions.
AND
-A subclass shouldn’t weaken post-conditions.
What are the 6 principles of LSP? #5
Invariants of a superclass must be preserved.
What are the 6 principles of LSP? #6
A subclass shouldn’t change values of private fields of the superclass.
What is the Interface Segregation principle (ISP)?
If a subclass does not need all methods, at these methods in an interface and link it only to the subclasses that needs these methods (a class can inherit from 1 class, but it can inherit from many interfaces)
What are the 3 conditions of the Dependency Inversion principle?
-High-level classes should not depend on low-level ones, but they should both depend on abstractions.
-Abstractions shouldn’t depend on details.
-Details should depend on abstractions.
What are high-level classes?
They have complex business logic directing low-level classes to do something.
What are low-level classes?
Implement basic operations like connecting to data base, transferring data, …