Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an example of the Single Responsibility Principle? [closed]

Can someone give me an example of the Single Responsibility Principle? I am trying to understand what it means, in practice, for a class to have a single responsibility as I fear I probably break this rule daily.

like image 641
Sachin Kainth Avatar asked May 16 '12 13:05

Sachin Kainth


People also ask

What is single responsibility principle example?

For example, if we have a class that we change a lot, and for different reasons, then this class should be broken down into more classes, each handling a single concern.

How do you use Open-Closed Principle?

In a nutshell, the developer must need to change only a specific part of the code (a class or a function) every time a requirement changes. Using a statically typed language like Java, C#, etc. the open/closed principle is generally achieved by using inheritance and polymorphism.

Which of the following is Open-Closed Principle?

Definition of the Open/Closed Principle Bertrand Meyer wrote about it in 1988 in his book Object-Oriented Software Construction. He explained the Open/Closed Principle as: “Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”

What is the issue with the single responsibility principle?

A class should have only one reason to change. A common misconception about this principle is that people think it means that a class should do only one thing. This is not the case: a class can do multiple things. If not, you'd end up with classes that only have a single public method.


1 Answers

The most effective way to break applications is to create GOD classes. Those are classes that keep track of a lot of information and have several responsibilities. One code change will most likely affect other parts of the class and therefore indirectly all other classes that use it. That in turn leads to an even bigger maintenance mess since no one dares to do any changes other than adding new functionality to it.

The following example is a TypeScript class that defines a Person, this class should not include email validation because that is not related with a person behaviour:

class Person {     public name : string;     public surname : string;     public email : string;     constructor(name : string, surname : string, email : string){         this.surname = surname;         this.name = name;         if(this.validateEmail(email)) {           this.email = email;         }         else {             throw new Error("Invalid email!");         }     }     validateEmail(email : string) {         var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;         return re.test(email);     }     greet() {         alert("Hi!");     } } 

We can improve the class above by removing the responsibility of email validation from the Person class and creating a new Email class that will have that responsibility:

class Email {     public email : string;     constructor(email : string){         if(this.validateEmail(email)) {           this.email = email;         }         else {             throw new Error("Invalid email!");         }             }     validateEmail(email : string) {         var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;         return re.test(email);     } }  class Person {     public name : string;     public surname : string;     public email : Email;     constructor(name : string, surname : string, email : Email){         this.email = email;         this.name = name;         this.surname = surname;     }     greet() {         alert("Hi!");     } } 

Making sure that a class has a single responsibility makes it per default also easier to see what it does and how you can extend/improve it.

like image 101
Remo H. Jansen Avatar answered Sep 30 '22 21:09

Remo H. Jansen