Design Principals of Object Oriented Programming Flashcards

1
Q

What is the shorthand for the Design Principals of OOP?

A

SOLID

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

What is the purpose of SOLID?

A

To make OOP code more understandable, flexible, and maintainable

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

What does SOLID stand for?

A
  • Single Responsibility
  • Open/Closed
  • Liskov Substitution
  • Interface Segregation
  • Dependency Inversion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Single Responsibility principle?

A

Every class should have only one responsibility

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

What is the main benefit of the Single Responsibility principle?

A

Code is modular and focussed

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

Can you describe an example of Single Responsibility principle in C#

A

Customer class with customer details and calculateDiscount() method.

The method is not performed by Customer class so should be put into it’s own class Discount

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

What is the Open/Closed principle?

A

A class should be open for extension but closed for modification

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

What is the main benefit of the Open/Closed principle?

A

Less impact on changes made in the code

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

Can you describe an example of Open/Closed principle in C#

A

We now have a class called Discount with out **calculateDiscount() **method. We may have different types of customers, so make **abstract **class with virtual function

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

What is the Liskov Substitution principal?

A

Every child class should be able to replace it’s parent class easily

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

What is the main benefit of the Liskov Subsitution principle?

A

Helps us create good inheritance hierarchies

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

Can you describe an example of Liskov Substitution principle in C#

A

What if we now have a Referral Customer? They aren’t actually a customer yet so won’t have a discount! So calculateDiscount() function does nothing.

Solution - create **CustomerBase **class without **calculateDiscount() **method and it in derived class.

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

What is the Interface Segregation principal?

A

No code should depend on methods it does not use

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

What is the main benefit of the Interface Segregation principle?

A

Looser coupling

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

Can you describe an example of Interface Segregation principle in C#

A

A Data Access Layer has CRUD methods defined and SQLServer, Oracle implementations.

However, one our implementations is just for a Reporting System so just needs Read method.

So we create interfaces IRead(Read method) and IDal : IRead(Add method, Update method, Delete method)

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

What is the Dependency Inversion principal?

A

Higher level modules should not depend on lower level modules directly but through abstraction

17
Q

What is the main benefit of the Dependency Inversion principle?

A

Helps to decouple

Great for testing

18
Q

Can you describe an example of Dependency Inversion principle in C#

A

Back to our Customer example. If we need to change logic in the Discount class we have to use IF statements. We should instead have an IDiscount interface with different implementations. We can use interface instead.

19
Q

Does Dependency Inversion solve making code loosely coupled?

A

No, as you still need to create instances of the objects

20
Q

What Design Patterns in C# can we use to decouple code?

A
  • Factory
  • Dependency Injection

See upcoming cards on Design Patterns