Friday, March 17, 2023
HomeJavaWhat's SRP or Single Duty Precept Instance in Java? SOLID Design Sample...

What’s SRP or Single Duty Precept Instance in Java? SOLID Design Sample Instance


Hiya guys, if you wish to find out about SRP or the Single Duty precept and questioning find out how to write code that follows SRP and what are professionals and cons of following this design precept then you will have come to the suitable place. Earlier, I’ve shared the greatest object-oriented programming and design programs and books and on this article, I’m going to speak about SPR. The Single Duty Rules, popularly often known as SRP is a part of 5 Object-oriented design rules, launched by Robert C. Martin, popularly often known as Uncle Bob. The Single Duty precept says that “there needs to be one motive to alter a category”. This implies a category ought to do just one factor, and do it completely.
SOLID is an acronym, the place S represents the Single Duty precept, O represents Open Closed design precept, L is for Liskov Substitution, I is for Interface segregation and D represents the Dependency Inversion design precept.

You may also examine my earlier submit about 10 Object Oriented and SOLID design rules for Java Programmer, for a fast evaluate of these rules. Mixed with core Object-oriented ideas e.g. Abstraction, Encapsulation, Inheritance, Polymorphism, and Composition, these rules assist you write higher, modular, versatile, and straightforward to handle code.

Uncle Bob has shared loads of recommendations on creating higher code in his e book Clear Code, which is price studying for any Programmer or developer. On this Object-Oriented design tutorial, we are going to take a better take a look at the Single Duty design Precept. We are going to see intent, advantages, drawbacks, and a few examples to study SRP SOLID precept.

Why use SRP or Single Duty Precept? Advantanges

Listed below are some key benefits and advantages of following SRP or Single Duty Rules in Java and Programing:

1. The principle benefit of SRP or Single Duty Precept leads to extremely cohesive code, as a result of courses simply do one factor, and a lot of the strategies have a tendency to make use of all of the occasion variables declared within the class since they’re intently associated to one another.

2. One other advantage of utilizing the Single Duty Precept is Separation of concern, Because the class cannot do multiple factor, they naturally separate issues, which in any other case must mingle within the class.

3. Another benefit of the SRP SOLID design precept is flexibility. It permits you to incorporate a change in software program with minimal affect. Since, if each class does only one factor, then change can be restricted to that class solely.

4. Single Duty Rules additionally improve the readability of code. It promotes higher names, and since class tends to do one factor, there are fewer probabilities of shock, to see code doing issues aside from its identify suggests.

What is SRP of SOLID Design Principle? Single Responsibility Principle Example in Java

Drawbacks of the Single Duty Precept

1. One of many major disadvantages of the Single Duty or SRP precept of SOLID design is just too many courses. It is simple to hold away with SRP and creates too many small courses. Even Uncle Bob has suggested in opposition to this in his e book Clear Code. Ideally, it is advisable to use a little bit of widespread sense whereas utilizing the SRP design precept and ensure you do not carry away with it.

2. One other drawback of the SRP SOLID design precept is elevated coupling. Since class simply do one factor, they have an inclination to rely on others for complimentary issues. Although Builders ought to attempt to decrease this coupling by not together with unrelated courses collectively. Even if you happen to are likely to rely on different courses, be sure that they’re logically associated.

SRP and Single Duty Precept in Java Code Instance

There are loads of locations the place you see a violation of the SRP design precept. One instance is from the Clear Code e book itself the place a category Worker not solely represents Worker information but additionally find out how to generate Worker reviews. 

Each time there’s a change on the report, the Worker class is modified which is a violation of the Single Duty Precept.  To repair this, you may transfer out report era logic from the Worker object to a separate Report object. 

Right here is one other, related instance of a Pupil class that violates the SRP precept. This class is liable for not solely storing scholar information but additionally calculating charges, reporting attendance, and saving Pupil information to the database. Each time there’s a change in payment calculation logic this class might be modified. 

public class Pupil {
    non-public String firstName;
    non-public String lastName;
    non-public int age;

    public lengthy calculateFee();
    public void save();
    public String reportAttendance();
}

}

To repair this, you may transfer out totally different functionalities into totally different courses, for instance, you may create a FeeCalculator class which might take an Worker object and calculate the payment for her or him. 

Equally, you may create AttendanceCalculator class to calculate attendance and a Persister class to save lots of scholar objects. 

public class Pupil {
 non-public String firstName;
 non-public String lastName;
 non-public int age;

	
}

public class FeeCalculator{
   public lengthy calculateFee(Pupil s){
     
   }
}

public class AttendanceCalculator{
   public String reportAttendance(Pupil s);{
     
   }
}

public class StudnetPersister{
   public void save(Pupil s){
     
   }
}


That is all on these SOLID and Object-Oriented design tutorials, we have now realized concerning the Single Duty Precept or SRP. Now we have seen the advantages and downsides of SRP, and a few examples of the Single Duty Precept. 

Although it is one of the vital primary and helpful OOD rules, you shouldn’t get carried away with this, do not take it to the extent, the place you finish of with many small courses. Maintain your variety of courses and bundle small.

Different Object-Oriented Programming tutorials chances are you’ll like

  • 5 Free Programs to study Object Oriented Programming (programs)
  • How you can design a Merchandising Machine in Java? (questions)
  • Distinction between Manufacturing unit and Summary Manufacturing unit Sample? (instance)
  • 7 Greatest Books to study the Design Sample in Java? (books)
  • When to make use of Command Design Sample in Java (instance)
  • Distinction between State and Technique Design Sample in Java? (reply)
  • 20 System Design Interview Questions (listing)
  • 7 Greatest Programs to study Design Sample in Java (programs)
  • How you can create thread-safe Singleton in Java (instance)
  • Distinction between Manufacturing unit and Dependency Injection Sample? (reply)
  • 18 Java Design Sample Interview Questions with Solutions (listing)
  • 10 OOP Design Precept Each Java developer ought to study (strong precept)
  • 5 Free Programs to study Knowledge Construction and Algorithms (programs)
  • How you can implement the Technique Design Sample in Java? (instance)

Thanks for studying this tutorial to this point. If you happen to like this tutorial then please share with your mates and colleagues, in case you have any suggestions or suggestion then please drop a remark. If you happen to prefer to study extra about different SOLID design rules like Liskov Substitution Precept then simply learn Clear Code.



RELATED ARTICLES

Most Popular

Recent Comments