Software Design Principles Flashcards
What is the purpose of the SOLID principles of Design and what are they used for?
The SOLID principles tell us how to arrange our functions and data structures into groups called classes, and how those classes should be interconnected so that they are easier to understand and better at tolerating change.
They are use at the midlevel to design the software that goes into modules and components.
Name the SOLID principles
- Single resposnibillity principle
- Open/Close principle
- Liskov’s substitution principle
- Interface segregation principle
- Dependency inversion principle
Define the Single Responsibillity Principle
A module or class should have only:
- ONE responsibillity.
- ONE reason to change,
- be responsible to only ONE ACTOR.
Code that different Actors depend on should be seperated
How does the Single Responsibillity Principle improve our code?
SRP reminds us to ‘seperate concerns’ so that each module is only responsible for a single task and to a single actor.
The seperation of concerns results in ‘low coupling’, which means that a module can be updated or changed, without unintentionally changing or breaking code in ohter ‘related’ modules, therefore making code more flexible
When we couple the concerns of different actors into a single module, the module can’t be changed without effecting all of the actors. (high coupling)
How can we test for Single Responsibillity Violation?
By using the “The _______ _________ itself” test.
if it doesn’t make sense then behaviour needs to be seperated into a seperate class.
Another test is the “AND” test. “This module does ____ AND _____, AND _______. Every and could indicate the need for a seperate class.
How do we apply the Single Responsibillity Principle?
Gather together the things that change for the same reasons. Separate those things that change for different reasons.
State the Open Closed Principle?
Modules or classes should be OPEN to extension but CLOSED to modification
How can we apply the Open Closed Principle?
Inheritance: Through inheritence, we can extend a class’ functionality without modifying the original code.
Abstraction and Encapsulation: By making Some methods private it becomes closed to modification, but we can provide public methods that uses the private methods in different ways therefore extending their functionallity.
Define the Liskov Substitution Principle
Subtypes must be substitutable for their basetypes
OR
If object S is a subtype of object type T, then objects of type T may be replaced by objects of type S, without side effect.
OR
The abillity to replace any instance of a parent class with an instance of one of it’s child classes without negative side effects
provide an example of the Liskov Substitution Principle as it relates to a car object or class.
If a Tesla is a subtype of a class or object called car then you should be able to “drive” a Tesla. This means that if I have a method that takes in a car object and calls its drive method, I should be able to replace the car object with the Tesla object without having to change my function call
What does the Liskov Subsitution Principle mean for methods?
It means that any method calls we use on a parent we should also be able to use on a child of that parent without changing the method call
What is the main purpose of the Liskov Substitution principle?
To prevent the misuse of inheritance.
When should inheritance not be used accordsing to the LSP?
When you don’t have substitutabillity.
What can be used instead of inheritance to “extend” a class
Delegation and Composition
explain delegation as a way to access the functionality of another class.
Instead of inheriting from a class, one can simple deligate the functions of that class, by instantiating an object of the desired class as a value within the new class