Thursday, May 2, 2024
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


Hey guys, if you wish to study SRP or the Single Duty precept and questioning write code that follows SRP and what are professionals and cons of following this design precept then you will have come to the appropriate place. Earlier, I’ve shared the finest 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 called SRP is a part of 5 Object-oriented design rules, launched by Robert C. Martin, popularly often called Uncle Bob. The Single Duty precept says that “there must be one cause 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 can too test my earlier put up about 10 Object Oriented and SOLID design rules for Java Programmer, for a fast evaluation of these rules. Mixed with core Object-oriented ideas e.g. Abstraction, Encapsulation, Inheritance, Polymorphism, and Composition, these rules enable you write higher, modular, versatile, and simple to handle code.

Uncle Bob has shared a variety of tips about creating higher code in his e book Clear Code, which is value studying for any Programmer or developer. On this Object-Oriented design tutorial, we are going to take a more in-depth have a look at the Single Duty design Precept. We’ll see intent, advantages, drawbacks, and a few examples to be taught 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 ends in extremely cohesive code, as a result of lessons 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 carefully associated to one another.

2. One other good thing about utilizing the Single Duty Precept is Separation of concern, For the reason that 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 lets you incorporate a change in software program with minimal impression. Since, if each class does only one factor, then change could 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 apart from its title 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 lessons. It is simple to hold away with SRP and creates too many small lessons. Even Uncle Bob has suggested in opposition to this in his e book Clear Code. Ideally, it’s essential 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 depend on others for complimentary issues. Although Builders ought to attempt to reduce this coupling by not together with unrelated lessons collectively. Even when you are inclined to rely upon different lessons, be certain they’re logically associated.

SRP and Single Duty Precept in Java Code Instance

There are a variety 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 knowledge but additionally generate Worker experiences. 

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 possibly can transfer out report technology logic from the Worker object to a separate Report object. 

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

public class Scholar {
    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 possibly can transfer out totally different functionalities into totally different lessons, for instance, you possibly can create a FeeCalculator class which may take an Worker object and calculate the payment for her or him. 

Equally, you possibly can create AttendanceCalculator class to calculate attendance and a Persister class to avoid wasting scholar objects. 

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

	
}

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

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

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


That is all on these SOLID and Object-Oriented design tutorials, now we have realized in regards to 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 crucial 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 lessons. Hold your variety of lessons and package deal small.

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

  • 5 Free Programs to be taught Object Oriented Programming (programs)
  • Methods to design a Merchandising Machine in Java? (questions)
  • Distinction between Manufacturing unit and Summary Manufacturing unit Sample? (instance)
  • 7 Greatest Books to be taught 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 (checklist)
  • 7 Greatest Programs to be taught Design Sample in Java (programs)
  • Methods to create thread-safe Singleton in Java (instance)
  • Distinction between Manufacturing unit and Dependency Injection Sample? (reply)
  • 18 Java Design Sample Interview Questions with Solutions (checklist)
  • 10 OOP Design Precept Each Java developer ought to be taught (stable precept)
  • 5 Free Programs to be taught Information Construction and Algorithms (programs)
  • Methods to implement the Technique Design Sample in Java? (instance)

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



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments