Single Responsibility Principle Explained - SOLID Design Principles

Single Responsibility Principle Explained - SOLID Design Principles

Web Dev Simplified

4 года назад

184,158 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@Jack-un5im
@Jack-un5im - 18.01.2024 13:23

Whenever I see this guys videos I have two thoughts:

1. He's a good teacher/explainer...
2. How the hell does he get his hair so perfect looking?

Ответить
@kvelez
@kvelez - 31.12.2023 03:40

class CalorieTracker {
constructor(maxCalories) {
this.maxCalories = maxCalories;
this.currentCalories = 0;
this.calorieLogger = new CalorieLogger();
}

trackCalories(calorieCount) {
this.currentCalories += calorieCount;
if (this.currentCalories > this.maxCalories) {
this.calorieLogger.logCalorieSurplus(this.currentCalories, this.maxCalories);
}
}
}

class CalorieLogger {
logCalorieSurplus(currentCalories, maxCalories) {
console.log(`You went over ${currentCalories - maxCalories} calories!`);
}
}

let tracker = new CalorieTracker(2000);
tracker.trackCalories(500);
tracker.trackCalories(1000);
tracker.trackCalories(1500);

==============================================================================

// main.js
import logCalorieSurplus from './logger.js';

class CalorieTracker {
constructor(maxCalories) {
this.maxCalories = maxCalories;
this.currentCalories = 0;
this.calorieLogger = logCalorieSurplus;
}

trackCalories(calorieCount) {
this.currentCalories += calorieCount;
if (this.currentCalories > this.maxCalories) {
this.calorieLogger(this.currentCalories, this.maxCalories);
}
}
}

let tracker = new CalorieTracker(2000);
tracker.trackCalories(500);
tracker.trackCalories(1000);
tracker.trackCalories(1500);


// logger.js
export default function logCalorieSurplus(currentCalories, maxCalories) {
console.log(`You went over ${currentCalories - maxCalories} calories!`);
}

==============================================================================

Ответить
@tusharsingh7926
@tusharsingh7926 - 08.12.2023 08:01

Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

Ответить
@lestrae
@lestrae - 14.11.2023 21:05

Thanks!

Ответить
@olyalitvinova9213
@olyalitvinova9213 - 12.10.2023 23:21

Hey! does it mean that class better have no more than 1 method?

Ответить
@winningtech5
@winningtech5 - 05.10.2023 13:49

i think this violates the yangni principle

Ответить
@ThomasIkemann
@ThomasIkemann - 19.09.2023 14:05

Hi Kyle, stupid question. What if I have a class like Car that can drive, stop, turn etc. Would the single responsibility principle "force" me to create own classes for these methods? That does not make sense for me^^ All those methods like toString(), substring() etc. are gathered below the very same class String as well, or do I missunderstand something here?

I get the main point though, that a method should have one single reason only, so that if something has to change, you only change one spot of the class instead of one big giant method.

Ответить
@arinmovsesian
@arinmovsesian - 06.08.2023 18:26

prefect explanation

Ответить
@doniaelfouly4142
@doniaelfouly4142 - 15.04.2023 13:56

thanks

Ответить
@pyakz2906
@pyakz2906 - 08.04.2023 16:46

Do you even blink bro?

Ответить
@Michael89312
@Michael89312 - 08.04.2023 14:56

very good. cheers

Ответить
@ulfdellbrugge4300
@ulfdellbrugge4300 - 26.02.2023 20:02

I think it could be improved if the message was just returned or if the message function was dependency injected?

Ответить
@cinammondream
@cinammondream - 29.12.2022 13:43

This is great, thank you!! Amazing video!

Ответить
@ibrahimyoussef4489
@ibrahimyoussef4489 - 16.11.2022 11:23

of course we want

Ответить
@cotixmol
@cotixmol - 05.11.2022 22:12

Does this imply that a class can only have one method? It doesn't sound right.

Ответить
@dmytrodanko8592
@dmytrodanko8592 - 05.09.2022 08:56

ok but it is brake another SOLID principle that said - do not change if it made (open/close principle, you can't change some method if it in use)

Ответить
@clipsbyczar
@clipsbyczar - 13.08.2022 03:24

you literally have a video for eveything! Thank you so much!

Ответить
@tomaskot9278
@tomaskot9278 - 31.07.2022 14:30

The final class still accumulates trajectories and keeps track of the value, compares it with a limit and logs a warning. All what changed is that it does not call the system log function, but does it indirectly through a custom function (which is a good change, but not sufficient if you really want the SR rule).

Ответить
@james-ov2rn
@james-ov2rn - 16.07.2022 09:46

is this mean a class should only have single method ? eg: for a Cart class Add and Remove method should be in two separate classes ?

Ответить
@user-fh1do9xb4n
@user-fh1do9xb4n - 17.06.2022 14:30

Very concise and up to the point, bravo!

Ответить
@ariassingh462
@ariassingh462 - 10.05.2022 09:14

Having classes with one method literally defeats the purpose of using a class. I feel like cohesion is more important than strictly adhering to the single responsibility principle. In this example the trackCalories and logCalorieSurplus are cohesive, so I don’t see a problem with keeping them in the same class

Ответить
@daninmanchester
@daninmanchester - 25.04.2022 22:23

Should you not inject your logger, or maybe even use events for looser coupling?
Using an interface would allow you to inject a logger of any type and would be helpful for testing.
If you are likely to have multiple events, then I would think raising a max calories event would be best as you could hook all manner of handlers to this.

Ответить
@someonemight
@someonemight - 21.03.2022 23:14

Good explanation. However, IMO to really separate responsibilities, CalorieTracker should call a notifyCaloriesExceeded function and that function would contain the message to output/email.

Ответить
@dz28021406
@dz28021406 - 03.03.2022 01:20

Very good explanation... Well done mate.

Ответить
@xtremehackerzpro9511
@xtremehackerzpro9511 - 05.02.2022 21:20

SRP = High cohesion and low coupling

Ответить
@jglopez5855
@jglopez5855 - 17.01.2022 16:54

amazing tutorials, thank you
pd. please blink

Ответить
@VitorOliveirakw
@VitorOliveirakw - 28.12.2021 22:31

Your English is perfect thanks from Brazil 👏

Ответить
@leandroroberto443
@leandroroberto443 - 11.10.2021 20:13

Very well explained !!!

Ответить
@someone-gp8fm
@someone-gp8fm - 24.09.2021 03:22

Now what about the part where you're checking if the current calories is greater or equal to max calories, shouldn't this get handled by another class too? Because now you are doing to things adding the calories and validating the value, also sometimes having a single class to do a very small task seems to be overkill, what about having other methods in the class and each method takes a single responsibility?

Ответить
@this.channel
@this.channel - 14.08.2021 16:34

Finally! I understand the S in SOLID. Now for the rest of the acronym :P

Ответить
@gsriraj
@gsriraj - 12.08.2021 07:12

God bless you man

Ответить
@uzairsaqib9298
@uzairsaqib9298 - 06.08.2021 16:27

You are great man !!!

Ответить
@utkarshdixit7235
@utkarshdixit7235 - 28.07.2021 01:15

Just confused as to why are you not using the sponsor's service for your own website server, if it's more powerful than your current hosting service, on top of 1-year free hosting. I smell something fishy xD

Great video btw

Ответить
@michaborski7383
@michaborski7383 - 21.07.2021 13:49

thank you !

Ответить
@leokr4877
@leokr4877 - 20.07.2021 22:48

In this case, I actually think that the new code is harder to read and maintain, because one function (track calories) suddenly does two things: Tracking AND notifying.

Ответить
@yadusolparterre
@yadusolparterre - 12.05.2021 22:16

Another way to say it: if you don't see a "this" in it, then it should be outside of the class

Ответить
@caitlinmclaren2695
@caitlinmclaren2695 - 25.04.2021 02:51

Your videos are awesome, but unfortunately, you misunderstood Single Responsibility Principal, like most people. Please read Uncle Bob's Clean Architecture book.

Ответить
@wioetuw912
@wioetuw912 - 02.04.2021 16:51

But how do you decide what is a "single responsibility" or "one reason to change"? For example, you could argue that the CalorieTracker class is still responsible for at least two things: handling the tracking and defining what message to log in case the tracked calories exceed max calories. And you can take the argument even further. If a class and a method both need to satisfy the single responsibility principle, then how can a class ever have more than one method? Two different methods represent two different responsibilities so if they are both in the same class, the class must have at least two different responsibilities. Sure, the responsibilities of the class and the methods are on different levels of abstraction but then how do you know what is the correct way to split the program into layers of abstraction? I mean, otherwise I could just put everything into a function called runProgram and claim that the function satisfies the single responsibility principle because its only responsibility is running the program and it only ever needs to change if I wan't to change how the program works.

I know that at least part of the answer to these questions is that you just have to use common sense. But the (supposed) usefulness of having an explicitly stated "single responsibility principle" is itself a consequence of the fact that it's very difficult to make correct decisions based on "common sense" unless you are very experienced in whatever you are doing. So I think it would be nice if there was more guidance on how to actually use this and other similar principles in practice.

Ответить
@samueltorres2485
@samueltorres2485 - 25.03.2021 04:41

Just found this channel. Great content, you teach these complex concepts well.

Ответить
@vnm_8945
@vnm_8945 - 23.03.2021 20:47

I want to ask a simple question I just noticed. What's the difference between a function written using the "function" keyword as "function example() {}" and writing it as "example() {}"? Thank you.

Ответить
@alanklm
@alanklm - 16.03.2021 23:28

It doesn't makes sense. If I want to change both - the message and the format of the message i need to change it in two places.

Ответить
@vossert
@vossert - 11.03.2021 11:52

Hi Kyle,

Thank you so much for all the effort you put into this channel. You taught me so much already!

I prefer hands-on learning so I code along with all your videos. What is your VSC setup so you can run the js and see the output on the right side in developer tools / console? Is it a new node project every time?

Ответить
@hyfydistro
@hyfydistro - 17.02.2021 20:47

Ahh, a cousin to the MVC Pattern 🧐

Ответить
@wilraul9695
@wilraul9695 - 05.02.2021 02:51

clear explanation thank you

Ответить
@julienwickramatunga7338
@julienwickramatunga7338 - 06.01.2021 11:26

Nicely explained!

Ответить
@ZaidIrfanKhan
@ZaidIrfanKhan - 02.01.2021 01:11

Hello, I am getting an error: Cannot use import statement outside a module. I guess it's something to do with the settings.json file. What changes would I need to make in that file, in order to make this work in VS code console? Any help would be appreciated

Ответить
@ragilburhanudinpamungkas9571
@ragilburhanudinpamungkas9571 - 14.12.2020 14:45

Wold you like to make an express tutorial with typescript that implement SOLID design pattern? Thank you.

Ответить