SOLID principles and design patterns Flashcards
What are the 4 main features of OOP?
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
What are design patters?
Not solutions but more principles, ideas or descriptions of how some code should function to implement the ‘pattern’.
What are the three common types of design patterns?
- Creational patterns
- Structural patterns
- Behavioural patterns
What are creational patterns?
Concerned with creating objects as needed for the problem/situation faced.
What are structural patterns?
Concerned with describing the relationships among objects.
What are behavioural patterns?
Concerned with communication among objects.
What are the SOLID principles?
- Single-responsibility principle
- Open-closed principle
- Liskov substitution principle
- Interface segregation principle
- Dependency inversion principle
What is a short definition for Single-responsibility principle?
A class should only have a single responsibility.
What is a short definition for open-closed princple?
Software entities should be open for extension, but closed for modification.
What is a short definition for Liskov substitution principle?
Objects should be replaceable with instances of their subtypes without altering the correctness of that program.
What is a short definition for interface segregation principle?
Many client-specific interfaces are better than one general-purpose interface.
What is a short definition for dependency inversion principle?
Depend on abstractions and interfaces, not concrete objects
What is the explanation for Single-Responsibility Principle?
Concerns should be separated. Every class should be focused solely on one activity only, and the only reason to change it should be because that activity changes (excluding bug fixes)
Consider this example: You have a single class that is responsible for requesting data from a remote system and generating reports. When might you rewrite the code according to the Single-Responsibility Principle.
- if the remote system changes
- if the report format changes
Provide an explanation for the Open-Closed principle:
Software entities (class, modules, functions) should be open for extension but closed for modification.
Closed in the sense that it has a fixed API others can depend on.
Open in the sense that a component can be extended.