Software Design Principles Flashcards

1
Q

What is the purpose of the SOLID principles of Design and what are they used for?

A

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.

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

Name the SOLID principles

A
  1. Single resposnibillity principle
  2. Open/Close principle
  3. Liskov’s 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
3
Q

Define the Single Responsibillity Principle

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the Single Responsibillity Principle improve our code?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can we test for Single Responsibillity Violation?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do we apply the Single Responsibillity Principle?

A

Gather together the things that change for the same reasons. Separate those things that change for different reasons.

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

State the Open Closed Principle?

A

Modules or classes should be OPEN to extension but CLOSED to modification

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

How can we apply the Open Closed Principle?

A

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.

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

Define the Liskov Substitution Principle

A

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

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

provide an example of the Liskov Substitution Principle as it relates to a car object or class.

A

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

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

What does the Liskov Subsitution Principle mean for methods?

A

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

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

What is the main purpose of the Liskov Substitution principle?

A

To prevent the misuse of inheritance.

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

When should inheritance not be used accordsing to the LSP?

A

When you don’t have substitutabillity.

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

What can be used instead of inheritance to “extend” a class

A

Delegation and Composition

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

explain delegation as a way to access the functionality of another class.

A

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

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

What is polymorphism?

A

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

polymorphism can also be described as the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types.

Any object that can pass more than one IS-A test is considered to be polymorphic.

17
Q

Refering to the Liskov Substitution Principle, when can you safely substitute a subtype for it’s base type?

A

When method names and signitures are the exact same for both.

18
Q

Regarding inheritance, explain the “Diamond of death” pattern

A

The “diamond problem” (sometimes referred to as the “Deadly Diamond of Death” is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?

19
Q

Explain the Composite Reuse Principle

A

Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class.

20
Q

Why should you favour composition over inheritance?

A

inheritance heirarchies can become overly rigid, which can make it difficult to change or update code. composition is more flexible than inheritance

21
Q

What is the difference between composition and inheritence?

A

With inheritence a type is designed around what it is, with composition a type is designed around what it does

22
Q

What is meant by “Encapsulate what varies”?

A

Take what varies and “encapsulate” it so it won’t affect the rest of your code. The result? Fewer unintended consequences from code changes and more
flexibility in your systems!

23
Q

What is meant by “Program to an interface, not an implementation.”?

A

“Programming to an interface” means, that when possible, one should refer to a more abstract level of a class (an interface, abstract class, or sometimes a superclass of some sort), instead of refering to a concrete implementation.

It means focusing your design on what the code is doing, not how it does it. This is a vital distinction that pushes your design towards correctness and flexibility

means that when you are presented with some programming interface (be it a class library, a set of functions, a network protocol or anything else) that you keep to using only things guaranteed by the interface. You may have knowledge about the underlying implementation (you may have written it), but you should not ever use that knowledge.

24
Q

What is the difference between an interface and implimentation?

A

An interface is a contract between a module and the user of that module. The interface provides a signiture which tells the user what is needed to interact with the module and what can be expected from the module in return.

Example 1: Toaster - socket - power station: The socket is the interface between the toaster and the powerstation. The implementation of the toaster does not depend on the power station, it can use any kind of power source. The socket can also interface with many diffferent kinds of appliances making it polymorphic.

Example 2: Driver - steering wheel - engine: The steering wheel allows the driver to interface with the engine. The way in which the driver interfaces with the engine is not determined by the type of engine

25
Q

Define the Interface Segregation Principle

A

Code should not be forced to depend on methods that it doesn’t use.

26
Q

How is the Interface Segregation Principle realized

A

By splitting a big fat interface into smaller more specific interfaces that are more relevant.

27
Q

State the Dependency Inversion Principle.

A

High level objects should not depend on low level implementation

                                   or 

Abstractions should not depend on details

28
Q

How can we apply the Dependency Inversion Principle?

A

Create an Abstract class that handles high level functionallity, but with zero implementation details.

It splits the dependency between the high-level and low-level modules by introducing an abstraction between them. So in the end, you get two dependencies:

the high-level module depends on the abstraction, and
the low-level depends on the same abstraction.