![]() * Adding a new protocol will create the need to add code for parsing and serializing the content for each type of field If we keep only one class, each change for a responsibility might affected the other one. Another one would be for the content field.even if content is a string maybe we want in the future to support HTML or other for mats. ![]() If other protocol’s must be supported the objects should be serialized in another manner and çode should be added to support new protocols. We are going to use the IEMAIL interface and Email class have 2 responsibilities ( reason to change) one would be the use of the class in some email protocols such as POP3 or IMAP. Let’s assume we need an object to keep on email message. The single Responsibility Principle is a sample and intuitive principle, but in practice it is sometimes hard to get it right.Ī class should have only one reason to change. When we need to make a change in a class having more responsibilities the change might affect the other functionality related to the other responsibility of the class. Each class will handle only one responsibility and if in the future we need to make one Change we are going to make it in the class which handle it. This principles States that if we have 2 reasons to change for a class, we have to split the functionality in two classes. It’s important to keep in mind that this principle leads to many other nuances and concepts, and not only those that fall within SOLID, but others that should be known and able to apply to to get an application with a solid design.ġ) With the aid of a suitable diagram give two examples that suitable explain the single Responsibility principle (SRP) of OOPĢ) Give two benefits of the SRP in software development.Įxamples of responsibility principle (SRP) of OOP.ġ) Motivation: in this content, a responsibility is considered to be one reason to Change. One will be responsible for calculating and another one for painting: The solution to this problem is to divide the class in two, so that each class has a unique responsibility. In this design, it can be seen that, if there are changes in the GUI, we must modify the Rectangle class, then we are obliged to test again the other application that attacks the same class. Draw () has the responsibility to draw the rectangle itself.Area () has the responsibility to return the area of a rectangle.In this simple example, you can see a rectangle class that has two methods Area () & Draw (). This leads to a design that is fragile and difficult to maintain, with all that that entails. If on the contrary, you build a class with more than one responsibility, what you’re doing is that you’re engaging these responsibilities. “A class should have only one reason to change.”īy following the single responsibility principle, you make sure that your class or module has high cohesion, which means that your class does not more than what it should do. Martin as a perfect definition of what is the Single Responsibility Pinciple: That is why I consider the phrase of Robert C. Within the context of the Single Responsibility Principle, responsibility is defined as a reason to change. As its name suggests, it implies that a class or module must have a unique responsibility. The principle is actually a very simple concept to explain, however, difficult to implement. ![]() Martin in his book “ Agile Software Development, Principles, Patterns, and Practices”. We’re going to start with the first one, the Single Responsibility Principle (SRP) that is defined by Robert C. Today I would like to concentrate on the first principle in object oriented design ( single responsibility principle) but first let’s mention the 5 solid principles: The difference between a good developer and a bad one. The difference between an application that is easy to maintain and one that is not. In object-oriented programming, there are five basic principles (OOP principles, SOLID) that, properly applied, make the difference between a good and a bad design.
0 Comments
Leave a Reply. |