Solid principles: 1. Single responsibility principle

The principle of single responsibility says that each class should be responsible for one specific thing, there should be only one reason to modify the class.

This is the most important SOLID principle, thanks to it we will not have classes of monsters after 2000 lines😐, which will be responsible for everything, but we will have many small classes responsible for different parts of the program.

Large classes are always harder to maintain, develop and refactorize, building large classes always sooner or later leads to problems such that if our project will be a class and will be responsible for everything in the future expansion of this class, changing its functionality will also affect completely different areas of this class, which in large projects🖥 leads to the fact that they are simply impossible to expand and modify.

However, by creating many small classes, you increase readability, ease of expansion and, generally speaking, ease of managing the project code.

Let’s move to practical examples, an example of a class that breaks the principle of a single responsibility is the one given in the introduction:

What we have here… Products class, which is responsible for everything, for placing orders, customers, calculating products, adding products, number of variables at the top of the class also does not look inviting, I forgot to mention that the single responsibility principle applies not only to classes, but also to methods, so that one or several methods do not do everything, you also have to try to do the shortest methods, preferably only perform one operation.

And we have here a few methods that do everything. If we were to make a practical project, we could not leave ✋ it that way, it should look like this:

And whether it does not look better? We have specific classes and methods that are responsible for a specific part of the store’s logic, the code has become much more readable, easier to understand and modify, we also the names of classes and methods have been improved. Of course, you can probably improve something in every code, no matter how well it would be written, how easy it would be to read, if all the rules of programming were met, not only SOLID, you can always improve something.

But you will admit that this is a much better solution than the one in which the Single Responsibility principle does not apply.

And below the call in the Main function:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

namespaceSingleResponsibility

{

classProgram

{

staticvoidMain(string[]args)

{

doubleMyWallet=30;

Client client=newClient("Slawomir","Kowalski","81-198",MyWallet);

ClientCart clientCart=newClientCart();

clientCart.Products=newList<string>();

clientCart.TypesProducts=newList<string>();

Shop shop=newShop(100);

shop.NewClient();

clientCart.AddProduct("cola");

clientCart.SaveTypeProducts("drink");

clientCart.SumProductsPrice(5.6);

clientCart.AddProduct("sprite");

clientCart.SaveTypeProducts("drink");

clientCart.SumProductsPrice(3.5);

clientCart.AddProduct("candy");

clientCart.SaveTypeProducts("candys");

clientCart.SumProductsPrice(1.4);

Order order=newOrder();

Console.WriteLine(order.GiveOrder()+

"\nThe number of products in the cart: "+clientCart.GetProductsCount()+