![]() When we create the contact object, behind the scene, the constructor method is called and initializes user data. Well, this is done by the constructor method. The first responsibility of the class is to create a contact object. Now we would go through each and every responsibility. Notice these methods are responsible for doing specific jobs. We have yet another method for sending emails. So we have another method for data validation. Because users can put in some malicious data that we do not want to accept. As we are dealing with user data, we must validate those data. Then we have a constructor method where we initialize user data. Our Contact class has an attribute that holds user data. ![]() We need to involve ourselves to figure out the meaning of the word responsibility. Otherwise, R of SRP. With the Contact class in place, we would start looking into our Contact class in the context of the Single Responsibility Principle. So we have three behaviors because we have three methods in our class. Behaviors perform different tasks for the class. In the context of a class, methods are behaviors. They are the methods of the Contact class. By the way, did you notice what behaviors does this class have? Can you figure them out? I know you can. Now we have our Contact class ready for our contact app. Have you really kept in mind that software is all about behavior? Okay! Say, we have been asked to build a simple web app through which users can contact the business owner. Now, we would explore SRP through the next sections. Later, Uncle Bob in his book, shifts that meaning a bit and relates Cohesion to the forces that cause a module or a class to change. In contrast, low Cohesion makes code difficult to maintain, test, and understand. These are actually very desirable traits in making an application. Because it makes code robust, reliable, reusable, scalable, maintainable, and understandable. So Cohesion tells how strong or loose the relationship between methods and data is in a class.Ī class or a module with high Cohesion is preferable. We know a class contains its methods (behaviors) and data. What is this? Cohesion measures the strength of the relationship between things that a module or a class contains. Before knowing it, I want you to keep in mind: Software is all about behavior. This principle was first described by Tom DeMarco² and Meilir Page-Jones³. The Single Responsibility Principle is the first one of the five principles. These principles have received wide attention in the software industry. They are collectively known as the SOLID Principles. ![]() He describes five OOD principles in his book. Robert Cecil Martin¹ (widely known as Uncle Bob) introduces a number of principles in his famous book:Īgile Software Development, Principles, Patterns, and Practices It is one of the principles of Object-Oriented Design. SRP stands for Single Responsibility Principle. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |