L4 Solid Principles Flashcards

1
Q

What are the SOLID Principles

A

Introduced in the early 2000s by Robert C. Martin

Tells us how to arrange our functions and data structure into classes and how those classes should interconnect

Principles are:
- Single Responsibility Principle (SRP)
- Open - Closed Principle (OCP)
- Liskov Substitution Principle (LSP)
- Interface Segregation Principle (ISP)
- Dependency Inversion Principle (DIP)

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

What does it mean to implement Single Responsibility Principle

A

A class should have only one responsibility

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

What does it mean to implement the Open-Closed Principle

A

Classes should be open for extension but closed for modification

● Modifying a class increases risk of breaking already working code
● To make our calculator extensible, we need to make it more generic using interfaces
● Following OCP we can now add any new operation without changing ANY class

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

What is the Liskov Substitution Principle

A

● Barbara Liskov: “If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behaviour of P is unchanged when o1 is substituted for o2 then S is a subtype of T”

The Liskov Substitution Principle (LSP) is a fundamental principle of object-oriented programming that states that objects of a superclass should be able to be replaced with objects of a subclass without affecting the correctness of the program. In simpler terms, if you have a class A and a subclass B, you should be able to use B wherever you can use A without causing any problems.

● Definition is quite verbose, so the classical square/rectangle problem:

● Where is the problem?
○ LSP says that User should be able to use Rectangle and
Square in the same way, but…
○ Square has the same height and width - calling
setHeight(10) and setWidth(10) is possible, but is waiting for
a disaster
○ So we introduce setSide(double) which in its body calls setHeight() and setWidth() with the
same parameter
○ LSP is broken, as User has to treat Rectangle and Square differently!
○ In other words, Square cannot be substituted wherever Rectangle shows up

● So, is Square a Rectangle?
○ Almost always no!
● LSP is a nice reminder that what is an “is-a” relationship in the “real world” is
not necessarily an “is-a” relationship in OOP world

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

What is Interface Segregation Principle

A

● A client should not depend on methods they do not use

● Let’s say Client 1 only uses op1(), Client 2 uses op2() and Client 3 uses op3()

● Client 1 depends on op2 and op3 even though it doesn’t call them

● If op2() or op3() change, Client 1 will still need to recompile and be redeployed into production

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

What is Dependency Inversion Principle

A

High-level modules or classes should not depend on low-level modules or classes, but both should depend on abstractions. In other words, the principle suggests that it’s better to depend on abstract interfaces or classes rather than concrete implementations

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

What is coupling and what is decoupling?

A

Coupling refers to the degree to which different modules or components in a system rely on each other.

Decoupling is the process of reducing coupling between different parts of a program. This can be achieved by breaking down a program into smaller, more modular components, each with well-defined interfaces that minimize dependencies between modules.

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

Flow of control and source dependency

A

● Any computer program will have a procedure function call tree, e.g

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

Why does architecture matter?

A

● High level policy depends on low level details
○ HL1() breaks if LL1() breaks
● Compile dependencies
○ Small change in LL1() forces everyone up the tree to recompile!
● How can we structure software better?
- SOLID principles

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

SRP friendly Employee

A

● A good general solution is to separate code that supports different actors (i.e. decouple)
● For our Employee, most solutions will require moving the methods into different classes

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

Interface Segregation Principle
Solution

A

● Alternative solution is to depend only on what we use
● Notice that OPS didn’t change
○ However, change to op1() doesn’t cause clients to
recompile
● Once again interfaces to the rescue
○ With interfaces we inverted source code dependencies
(OPS is pointing towards the interfaces), but the flow
of control remained the same (from Clients to OPS)
● In general, do not depend on something that
carries baggage that you don’t need

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

Interface Segregation Principle (ISP)
Effect of ISP

A

ISP has inverted source code dependency (many OOP design
techniques rely on this trick!)

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

Dependency Inversion Principle (DIP)
Rule of Thumb

A

○ Don’t refer to volatile concrete classes (i.e. classes that change very often)
○ Don’t inherit from volatile concrete classes
○ Don’t override concrete functions
○ Avoid mentioning the name of anything concrete and volatile

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

Dependency Inversion Principle (DIP)
Problem

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

Dependency Inversion Principle (DIP)
Solutions

A

● Each time the requirement changes, we need to modify Person
● But, by programming to an interface, we can avoid changing the Person!
● Person can now save into any database which satisfies the Database interface!

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