One example of the SOLID principles in Java is the Single Responsibility Principle (SRP). It states that a class should have only one reason to change, meaning it should only have one responsibility or function.

For instance, consider a Report class. If this class handles generating the report and also saving it to a file, it violates SRP. Changes to the file-saving logic could unexpectedly affect the report generation.

To follow SRP, split responsibilities into separate classes:

class Report {
public String generateReport() {
return "Report Content";
}
}

class ReportSaver {
public void saveToFile(String report, String filename) {
// Logic to save report to a file
}
}

Now, changes to saving logic don’t affect report generation. This makes the code easier to maintain and extend, adhering to good software design practices.

Hinterlasse einen Kommentar

I’m Iman

Mein Name ist Iman Dabbaghi. Ich arbeite als Senior Software Engineer in der Schweiz. Außerdem interessiere ich mich sehr für gewaltfreie Kommunikation, Bachata-Tanz und Musik sowie fürs die Persönlichkeitsentwicklung.

Ich habe einen Masterabschluss in Informatik von der Universität Freiburg in Deutschland, bin Spring/Java Certified Professional (OCP), Certified Professional for Software Architecture (CPSA-F) und ein lebenslanger Lernender 🎓.

EN:

My name is Iman Dabbaghi. I work as a Senior Software Engineer in Switzerland. I am also very interessted in nonviolent communication, Bachata dance and music and also for personal development.

I hold a masters degree in computer science from the university of Freiburg in Germany, am a Spring / Java Certified Professional (OCP), Certified Software Architecture (CPSA-F) and Life Long Learner🎓

Let’s connect