SOLID New Flashcards

1
Q

What does the Single Responsibility Principle state?

A

A class, module, or method should have only one reason to change and should focus on a single functionality.

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

What are common signs of a Single Responsibility Principle (SRP) violation?

A

A class serves more than one actor, has responsibilities for multiple functionalities, or contains entangled methods that serve different purposes.

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

What are the problems caused by SRP violations?

A

Classes become tightly coupled, changes in one part can affect the entire system, and they can lead to ‘God classes’ that know or do too much.

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

What is an example of SRP violation?

A

A class like DLModel that includes preprocess, train, and evaluate methods serving three different functionalities.

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

How can you improve SRP violations in the DLModel example?

A

Separate the functionalities into individual classes: Preprocessor for preprocess, DLModel for train, and Evaluator for evaluate.

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

What does the Open/Closed Principle state?

A

Software entities (classes, modules, methods, etc.) should be open for extension but closed for modification.

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

What is a common sign of an Open/Closed Principle violation?

A

Classes containing switch or if statements that require modification when adding new functionality.

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

What is the benefit of adhering to the Open/Closed Principle?

A

You can extend functionality without altering existing code, making the codebase more stable and maintainable.

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

What does the Liskov Substitution Principle (LSP) state?

A

Objects of a superclass should be replaceable with objects of a subclass without altering the correctness of the program.

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

What is an example of an LSP violation?

A

A Square class inherits from a Rectangle class but violates the ‘is-a’ relationship due to differences in how they calculate area (e.g., width × height vs. side × side).

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

How can LSP violations be fixed in the Rectangle-Square example?

A

Create a Shape abstract class with a calculate_area method and let Rectangle and Square extend this class, implementing their specific logic.

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

What does the Interface Segregation Principle state?

A

Clients (classes and subclasses) should not depend on methods they do not use. It’s better to have many specific interfaces than a single general one.

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

What is an example of an Interface Segregation Principle violation?

A

An OldPrinter class inherits from a Printer interface with methods like print, scan, and fax, but OldPrinter does not need scan and fax.

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

How can ISP violations be fixed in the Printer example?

A

Create smaller interfaces like Print, Scan, and Fax, and let OldPrinter implement only the Print interface while NewPrinter implements all three as needed.

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

What does the Dependency Inversion Principle (DIP) state?

A

Abstractions should not depend upon details; details should depend upon abstractions.

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

What is a common sign of a Dependency Inversion Principle violation?

A

A class depends on a concrete class (e.g., instantiating it in its constructor), rather than depending on an abstraction or interface.

17
Q

What is the main benefit of adhering to the Dependency Inversion Principle?

A

Code becomes loosely coupled, easier to test, and more robust to changes in concrete implementations.

18
Q

How can DIP violations be resolved in the MLPipeline example?

A

Create an Evaluator interface and let MLPipeline depend on this interface. Concrete implementations like PyTorchEvaluator or TensorFlowEvaluator can implement the interface.

19
Q

What technique is commonly used to follow the Dependency Inversion Principle?

A

Dependency injection is commonly used to inject abstractions into classes instead of directly instantiating concrete objects.

20
Q
A