SOLID principles Flashcards

1
Q

What is the DRY principle?

A

DRY (Do not Repeat Yourself)
- avoid duplication: a piece of data has a single value stored on the system. Don’t want to store duplicated data
- centralise logic to make it easier to update/test/understand
- write concise, reusable code, this saves time

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

What are some violations of the DRY principle?

A
  • storing repeated data across db tables
  • storing derived data across db tables
  • storing same data at client and server
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 5 SOLID principles?

A

1) Single Responsibility
2) Open Closed principle
3) Liskov substitution principle
4) interface segregation principle
5) Dependency inversion principle

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

What do the SOLID principles help do?

A

They are design principles for creating OO code code that is likely to have less problems with rigidity, fragility, immobility and viscosity

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

Explain the first principle of the SOLID principles:

A

Single responsibility: a class should only have one responsibility
- why? if it has multiple, changes to one responsibility could impact the other

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

When detecting a breach of the single responsibility principle, what should you look for:

A

When a class is handling multiple things:
e.g the word processor class is responsible for saving a document, loading a document and spell checking a document, these should be separated into a SpellChecker class and a fileHandler class

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

What are the benefits of single responsibility?

A
  • Reduces coupling
  • easier to test, understand and maintain code
  • improves code reusability
  • several developers can work on coding classes simultaneously (due to low coupling)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Open Closed principle:

A

Classes are 1) open for extension and 2) closed for modification
- why? you should be able to add new functionality to a class without altering it’s existing code

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

How is the open closed principle achieved?

A

Through abstraction, abstract classes and interfaces, and dynamic or static polymorphism
(inheritance and overriding methods)
- Use the word final in constructors/method definitions

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

When detecting a breach of the open closed principle, what should you look for:

A

class DiscountCalculator:
def calculate_discount(self, customer_type, amount):
if customer_type == “Regular”:
return amount * 0.05
elif customer_type == “Premium”:
return amount * 0.10
elif customer_type == “VIP”:
return amount * 0.20
else:
return 0

Having if, elif, elif and else that all return different values
- if a new customer_type needs to be added another elif statement is added, this violated the OC principle, because the class should just be extended to add another customer type

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

What are the benefits of the open closed principle?

A
  • ## The current code base is kept stable and bug free but allows it to be used for future code development
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the final keyword mean?

A

That a method or class can’t be overridden

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

What would be the structure for a correct implementation of the open closed principle:

A
  • public interface IcardProvider {
    public boolean makePayment(string number)
    }
  • public class cardProviderBase implements IcardProvider { //closed for modification
    public final boolean makePayment(string number) {
    return number }
    }
    protected boolean onMakePayment(String number) { //open for extension
    return(false)
    }
    }
  • public class natwestCardProvider extends CardProviderBase{
    protected boolean
    onMakePayment(String number) {
    return(true);}
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are signs that the open closed principle has been breached?

A
  • Adding new if/elif conditions to existing methods to handle new cases.
  • Modifying existing classes to add new functionality instead of creating new classes or methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Liskov substitution principle:

A

Classes must be substitutable for their bases classes
- why: subclasses should adhere to the behaviour expected of their parent class

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

What are the benefits of the liskov principle?

A
  • prevents unexpected behavior in inheritance hierarchies
  • improves code reliability
17
Q

When detecting a breach of the liskov substitution principle, what should you look for:

A

A subclass that doesn’t have the method of a superclass or doesn’t make use of that method.

e.g. a Class Bird has method fly() is extended by Class Penguin which can’t fly

18
Q

How can a class the breaches the liskov substitution principle be fixed?

A

By having the subclass match the superclasses methods or by splitting the superclass into more classes with differing functionality and having the subclass inherit from the correct one

e.g. Class bird should become classes flyingBird and nonFlyingBird and class penguin should then inherit from nonFlyingBird which won’t have method fly()

19
Q

What is the Interface segregation principle:

A

a class should not be forced to implement interfaces it doesn’t use
- because interfaces should be small and specific to avoid classes implementing unnecessary methods

20
Q

When detecting a breach of the interface segregation principle, what should you look for:

A

An interface with lots of methods, and a class implementing the interface and defining the method though it’s irrelevant

e.g a Bird interface having method fly() and a penguin class defining fly when it can’t fly

21
Q

What are the benefits of the interface segregation principle?

A
  • low coupling
  • codes is more flexible
  • makes classes easier to understand and implement
22
Q

How can a bad implementation of the interface segregation principle be fixed?

A

Splitting a large interface into two or more smaller interfaces, so base classes that implement the interface actually require all the methods they need to define

e.g. split Bird interface into nonflyingBird interface with methods eat() and flyingBird interface with methods eat() and fly()

23
Q

What is the dependency inversion principle:

A
  • high level modules should not depend on low level modules.
  • Both should depend on details
  • details should depend on abstractions
    why: means low and high level modules will communicate through interfaces/abstract classes instead of concrete implementation
  • if low level modules change, it won’t break high level modules
24
Q

What are the benefits of dependency inversion principle?

A
  • increases flexibility
  • easier to test
  • reduces coupling/dependency issues
25
Q

What should a bad implementation of the dependency

A
  • Instead of a DatabaseService class directly depending on a MySQLDatabase
  • it should depend on an interface like Database.
  • This allows switching to another database with minimal changes.
26
Q

What is polymorphism?

A

It allows objects of different types to be treated as instances of the same class through a common interface

27
Q

What is dynamic polymorphism?

A
  • achieved by method overriding
  • in method override a subclass provides it’s own implementation of a method that’s defined in the superclass
  • if a method is final it can’t be overridden in the subclass
  • resolved at runtime
  • open closed principle
28
Q

What is static polymorphism?

A
  • resolved at compile time
  • achieved through method overloading
  • in method overloading, classes have multiple methods with the same name but different parameter types or counts
  • the complier determines which method is used
  • open closed principle
29
Q

What are generics?

A
  • Allow you to write code without defining the object type being accessed
  • when the code is compiled it uses the ‘object’ type instead of the unknown type
30
Q

What is type erasure?

A

Removing the type of a generic class when it compiles, and replacing it with the object type
- then at runtime it replaces the object type with the type of the instance called

31
Q

What do the first 3 principles of SOLID focus on and what do the last 2 principles focus on?

A
  • The first 3 principles focus on designing code to stop things going wrong
  • the last 2 principles focus on designing code well
32
Q

What is design by contract?

A
  • methods have a contract which is checked. For each method you state things that must be true before that method is allowed to be called, and when that method is returned.
  • use keyword assert to say you want / expect something to be the case, e.g. you want the type to be equal to something or the value of a variable to equal something
33
Q

What is the top down approach structure for OO design:

A

Interface
|
abstract sub class
| |
concrete class concrete class