hello and welcome to pseudocode in this video we are going to talk about one of the solid principles that starts with s which is single responsibility principle you might have heard definitions like single responsibility principle means one class should have only one and one reason to change one class should have only one public method or if your class description has and in the definition that means you're breaking the single responsibility principle and so on i'm sorry to break the sad news to you this is not what single responsibility principle means let's see actually what uncle bob meant when he coined the term single responsibility principles or how he has explained in his book so let's get started [Music] single responsibility principle from its name sounds very straightforward that one class or one module or one function should have only one responsibility and should have only one reason to change in fact even i also used to think like that but on further digging i found this book by uncle bob it is called clean code and i'm going to use the same example from from his book that explains the single responsibility principle but to start with the definition that principle actually states any module module means a set of functions or a set of functionality a class or a package or it can be a set of functions or it can be a source code itself should have a reason to change by only one actor or the whole functionality should be able to change by only one actor now what does this mean here we can understand what is the module but what is actor here now let's try to understand that using an example there is a class called employee and that class has three different methods calculate salary calculate hours and save employee data now that calculate salary method can be used by actually the financial officer of a company so let let's call that person cfo the hours calculate hours data can be required by the human resources department of the company and saving the employee data might be required from the technical or engineering department so now we can see that there are three different actors or three different stakeholders who actually need this functionality which is all written in one class now let's imagine a scenario where calculate salary and calculate hours are using some same underlying function or a private function in a class which is called get hours or get regular hours now if there is any kind of calculation change in the calculate salary method which is required by the cfo this calculation uh function will change in this class and it might require a change in the regular hours private function also so the person who is making this change uh he or she made a change here also now two methods public and private both have been changed now since calculate hours is also using this regular regular hours method internally this implementation breaks so cfo required a change to be made in certain functionality and the human resources department is getting some wrong data because somebody changed the underlying implementation of a private method which was used by both these public methods so what actually happened here what happened here was one class exposed two or three different methods two or three different public methods which were corresponding to different stakeholders of the software or different actors of the software one actor does not have to know anything about the other actor or they might not know anything about the other actor but still change requested from one of them has impacted the other one now this here is the breaking of single responsibility principle because this module is encapsulating the different functions of different functionality which cater to different actors of the system and this is what single responsibility principle means that the code that you are writing if that code request changes from only one actor or one stakeholder or even group of one stakeholder which fulfill one business requirement till that point it is fine so that means your class can have more than one public methods as long as the change in those public methods or change in those business logic is requested by one primary stakeholder or group of stakeholders till then it is fine but if one class has changes being requested from different stakeholders that means you have tried to put toward three different business functionalities together in one module and you have broken the single responsibility principle so this is what the example in the clean code entails now how do we solve this the best way to solve this like in the book it has been explained using facade pattern which i will not go into detail here because it is still out of scope i have not completed the design patterns yet but a better way to do this is to break the classes you make a salary calculator class you make our calculator class and you making a store class and all those classes can be called from different parts of the program and the calculation of salary does not need to know about or does not need to depend on any method that is used in calculate hours similarly saving the employee data to some database does not need to know about calculate salary or calculate hours at all this way while by separating the class or by decomposing one class into multiple classes we can actually other to single responsibility principle where the business function or the business requirement or the business logic sits in particular classes and the request for those requests for changing that business logic can only come from one particular stakeholder since all of this can be a lot to wrap your head around hence we will try to understand one more simpler example let's say there is a small restaurant app where we have a class called menu item now this class has the name of the item the if that item is available for take away or not and the amount of the item and tax and discount etcetera okay now there is another class called bill which can consist a list of these items the customer on whose name the bill has to be generated the timestamp and other properties if you think about it the responsibility of the bill class is to calculate the final amount so what does this function does it goes over all the items of the classes get their prices applies particular discount etc and returns the final amount now once the bill is generated the bill also has to be printed and the same bill file of the same build data also has to be saved to some location it can be a local file or it can be a database or anything so there is a save function it can seem perfectly fine to keep all these methods in the same build class where you are calculating the bill printing the bill and saving the bill now let's see what happens there is a team who want to change the database implementation of how the bill is being saved maybe they want to change the data store all together or maybe they want to modify the columns the way in which the bill is saved they would come and make changes to this class now if they have to make changes to this class they might end up adding some more properties in the class implementation itself and whoever is using the constructor of this bill class that functionality will break because they have changed the attributes and the basic properties of this class itself and why did they change it because they wanted to change how the bill is saved in database so again saving something to database this responsibility should have been catered by a different class if let's say this function save bill would have been in an another class called store class and that implementation would have been hidden from the bill class whoever was supposed to make that change they would have made this change in the in that store class itself without having to change the any columns of the bill similarly printing the bill that format require that format print format might require some changes and again if though that change is impacting the whole bill class it is again breaking the single responsibility principle because so many things are happening inside one class and change in one method can impact another method so it would be so the best way would be to again decompose these classes there can be a uh bill printer class there can be a bill store class and then there can be a bill class whose sole responsibility is to calculate the final amount of the bill and once the final amount of bill is calculated this bill object can be passed on to the bill printer class and this bill object can be passed on to the storable class now however the store class wants to store the data add or remove columns or keep some data from the bill or not keep some data from the bill they can do it without impacting the actual build class so this comprises of the whole single responsibility principle i hope that i have explained it in a clear manner and uh please note that again and again i am saying that it does not mean that one class should have only one public method that is not the meaning of single responsibility principle the meaning is module collection of classes functions should cater changes to a particular business stakeholder or a group of stakeholders through which the changes come into the picture if one class or one module has changes coming in from different business functions that means something is going wrong and as i always say design is something which is very subjective it is not objective there is no single right or wrong answer to single responsibility principle you might argue that this way if i keep on decomposing my classes i will end up with a lot of classes and where do i stop again it is up to your judgment and the business requirements that how do you organize your classes decompose your classes or put them together as long as it is fulfilling the business requirement and it is rendering the software easy to change as long as the software is easy to change and it is not breaking the existing functionality that itself is a good signal that your design is good i have linked the book from which i have picked this example i have linked other resources as well for you to read more about this concept if you have any doubt please please feel free to leave it in the comments and i will try to address it in the next video we will understand the next solid principle starting from o which is called open close principle till then take care see you in the next video [Music]