Lecture 4-Software design principles Flashcards

1
Q

What is the solution to code reuse?

A

Design patterns : middle between reuse of classes at the lowest level, and reuse of frameworks at the highest level *(Hollywood principle)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is encapsulation?

A

Separating the code that varies from the code that stays the same, to minimize the effects caused by the changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 4 types of encapsulation?

A

-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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the open/closed principle?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Liskov Subsitution principle (LSP)?

A

Derived classes should extend without replacing the functionality of old classes.

AKA : when overriding, extend the base behaviour instead of fully replacing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 6 principles of LSP? #1

A

-Parameter types in a method of subclass should match or be more abstract than parameter types in the method of the superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the 6 principles of LSP? #2

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the 6 principles of LSP? #3

A

A method in a subclass shouldn’t throw types of exceptions that the base method isn’t expected to throw.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the 6 principles of LSP? #4

A

-A subclass shouldn’t strengthen pre-conditions.
AND
-A subclass shouldn’t weaken post-conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 6 principles of LSP? #5

A

Invariants of a superclass must be preserved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the 6 principles of LSP? #6

A

A subclass shouldn’t change values of private fields of the superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the Interface Segregation principle (ISP)?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the 3 conditions of the Dependency Inversion principle?

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are high-level classes?

A

They have complex business logic directing low-level classes to do something.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are low-level classes?

A

Implement basic operations like connecting to data base, transferring data, …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain relationship between high-level classes and low-level classes

A

High-level classes should not depend on low-level ones, but they should both depend on abstractions.