Bruh
Bruh
SESSION 12
- Introduction
• SOLID is an acronym for OOP principle which help gives a standard for ease of
maintenance for the application built using OOP approach.
• The purpose of using SOLID principle is to create software that is easy to maintain,
extend, and reuse. SOLID principle helps developers avoid code smells, reduce
dependencies, and improve readability and stability of the code.
• By following SOLID principle, developers can write software that is more flexible,
logical, and adaptable to changes
SOLID PRINCIPLE I
INTRODUCTION
• This session will talk from single responsibility until liskov substitution
SOLID PRINCIPLE I
SINGLE RESPONSIBILITY
• The single responsibility principle (SRP) states that every class, module, or function
should have only one responsibility or purpose in a program.
• This means that each class should have only one reason to change, and that reason
should be related to the class’s responsibility
SOLID PRINCIPLE I
SINGLE RESPONSIBILITY
• For example, suppose we have a class that handles student registration, result
calculation, and email sending. This class violates the SRP, because it has three
different responsibilities and three different reasons to change. If we want to modify
the registration logic, we might affect the result calculation or email sending logic.
This makes the code hard to maintain, test, and reuse.
• A better design would be to separate these three responsibilities into three different
classes, each with its own responsibility and reason to change. This way, we can
change one class without affecting the others, and we can reuse the classes in
different contexts. This makes the code more modular, readable, and stable
SOLID PRINCIPLE I
SINGLE RESPONSIBILITY EXAMPLE
// This class has two responsibilities: storing user data and validating user
input
• Here is a code block that shows public class User {
an example of a class that private String name;
violates the single responsibility private String email;
private String password;
principle:
public User(String name, String email, String password) {
this.name = name;
this.email = email;
this.password = password;
}
class have only one responsibility each, // This method is related to the validation logic
public boolean isValidPassword(String password) {
and they have only one reason to change
return password.length() >= 8;
}
}
SOLID PRINCIPLE I
OPEN CLOSED PRINCIPLE
• It states that software entities, such as classes, modules, or functions, should be open for
• This means that you can add new features or behaviors to a software entity without changing
• This way, you can avoid breaking the existing functionality or introducing bugs
SOLID PRINCIPLE I
OPEN CLOSED PRINCIPLE
• One way to achieve the open closed principle is to use interfaces or abstract classes to define
• Then, you can implement or inherit from these interfaces or abstract classes to provide
• This allows you to extend the functionality of your software entities without modifying the
• To follow the open closed principle, we should separate the area calculation logic from the shape classes.
• We can create an interface that defines a method for calculating the area, and implement it in each shape
class.
• Then, we can use polymorphism to invoke the appropriate method for each shape
•Now, the AreaCalculator class and the // This class has one responsibility: calculating the
total area of a list of shapes
shape classes have only one
public class AreaCalculator {
responsibility each, and they have only
one reason to change. This makes the public double calculateTotalArea(List<Shape> shapes) {
code more modular, readable, and double totalArea = 0;
maintainable. for (Shape shape : shapes) {
totalArea += shape.calculateArea(); // This
•If we want to add more shapes, we can will invoke the appropriate method for each shape
simply create a new class that }
implements the Shape interface, and return totalArea;
the AreaCalculator class will not need }
to change }
SOLID PRINCIPLE I
LISKOV SUBSTITUTION PRINCIPLE
• In other words, a subclass should behave like its superclass and not
introduce any unexpected behavior or side effects
SOLID PRINCIPLE I
LISKOV SUBSTITUTION PRINCIPLE (LSP)
• https://www.baeldung.com/java-liskov-substitution-principle