Design Principals of Object Oriented Programming Flashcards
What is the shorthand for the Design Principals of OOP?
SOLID
What is the purpose of SOLID?
To make OOP code more understandable, flexible, and maintainable
What does SOLID stand for?
- Single Responsibility
- Open/Closed
- Liskov Substitution
- Interface Segregation
- Dependency Inversion
What is the Single Responsibility principle?
Every class should have only one responsibility
What is the main benefit of the Single Responsibility principle?
Code is modular and focussed
Can you describe an example of Single Responsibility principle in C#
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
What is the Open/Closed principle?
A class should be open for extension but closed for modification
What is the main benefit of the Open/Closed principle?
Less impact on changes made in the code
Can you describe an example of Open/Closed principle in C#
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
What is the Liskov Substitution principal?
Every child class should be able to replace it’s parent class easily
What is the main benefit of the Liskov Subsitution principle?
Helps us create good inheritance hierarchies
Can you describe an example of Liskov Substitution principle in C#
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.
What is the Interface Segregation principal?
No code should depend on methods it does not use
What is the main benefit of the Interface Segregation principle?
Looser coupling
Can you describe an example of Interface Segregation principle in C#
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)