Dear student, Solid in Java real life... the S

Dear student, Solid in Java real life... the S

Dear student, in many Java Interviews your ll be ask talk for SOLID Principles, today we ll talk about S.

S represents the Single Responsibility principle O represents the Open Closed principle L represents the Liskov Substitution principle I represents the Interface Segregation principle D represents the Dependency Inversion principle

The SOLID principles are useful when constructing both individual modules or larger architectures. Ok, but ...

S it represent the old cohesion and lower coupling principles. Cohesion is the ammount the functional relatedness of the elements of a module. The S is useful for Testing (A class with one responsibility will have far fewer test cases), Lower coupling (will have fewer dependencies) and Organization of the code (Smaller, well-organized classes are easier to search and work)

Put together in practice this principle, you have a class Receipe

class Receipe... ...

public calculateCost()...

public printReceipe()... ...

May be, if you considerer the high cohesion principle, printReceipe is more about a technique to how print, more a I/O algortihm that a Receipe stuffs, then, applying this principle, we can remove this method for the class, and make a new class or interface, class ReceipePrinter, that receibe a Receipe and print it ;)

class Receipe...

... public calculateCost()...

class ReceipePrinter... public printReceipe(Receipe receipe)..