Most Common Design Patterns Concept: What are SOLID, DRY and ACID principles – Part 1
SOLID principles
This is more interview based concept if you are backend developer this is obvious to ask.
S: Single Responsibility Principle (SRP)
• Each part of your code should have one job.
• Makes code easier to read and maintain.
• Reduces the risk of bugs when making changes.
• Real-Life Example: A chef in a restaurant only focuses on cooking, not serving customers or managing finances.
• Coding Scenario: A class Invoice Printer should only handle printing an invoice, not calculating totals.
class InvoicePrinter {
public void print(Invoice invoice) {
// Code to print the invoice
}
}
O: Open/Closed Principle (OCP)
• Code should be open for extension but closed for modification.
• You can add new features without changing existing code.
• Helps prevent breaking things when adding new functionality.
-Life Example: A smartphone that lets you install apps without changing the base operating system.
• Coding Scenario: A Shape class can be extended to create new shapes without modifying existing code.
interface Shape {
double calculateArea();
}
class Circle implements Shape {
double radius;
public double calculateArea() {
return Math.PI * radius * radius;
}
}
L: Liskov Substitution Principle (LSP)
• Objects or sub classes should be replaceable with their parent classes without causing problems.
• Ensures that inherited classes work just like their parent class.
• Keeps code consistent and reliable.
Real-Life Example: If you can ride a bicycle, you should also be able to ride an electric bike without any trouble.
• Coding Scenario: A Bird class should be replaceable with its subclass Sparrow without breaking the program.
class Bird {
public void fly() {
// Generic flying logic
}
}
class Sparrow extends Bird {
@Override
public void fly() {
// Specific flying logic for a sparrow
}
}
I: Interface Segregation Principle (ISP)
• Don’t force a class to use methods it doesn’t need.
• Split big interfaces into smaller, more specific ones.
• Makes your code easier to use and implement.
• Real-Life Example: A remote control with buttons you only use, instead of unnecessary buttons for advanced features.
• Coding Scenario: Separate interfaces for different functionalities instead of one large interface.
interface Scanner {
void scan();
}
interface Printer {
void print();
}
class MultiFunctionPrinter implements Scanner, Printer {
public void scan() { /* scanning logic */ }
public void print() { /* printing logic */ }
}
D: Dependency Inversion Principle (DIP)
• High-level code should not depend on low-level code; both should depend on abstractions.
• Makes code more flexible and less tightly connected.
• Easier to change and test.
• Real-Life Example: A coffee machine that works with any brand of coffee pods.
• Coding Scenario: High-level modules depend on abstractions, not concrete classes.
interface NotificationService {
void sendNotification(String message);
}
class EmailService implements NotificationService {
public void sendNotification(String message) {
// Send an email
}
}
class UserNotification {
private NotificationService service;
public UserNotification(NotificationService service) {
this.service = service;
}
public void notifyUser(String message) {
service.sendNotification(message);
}
}
DRY
Dry (don’t repeat yourself) not common but have to understand when practical scenarios most of review comments are make because it this.
Just do once and reuse as much as you can.
If someone have develop the functionality that you need use it don’t go for making it again in your style, this is team contributed issue know.
Easier maintenance: Change in one place updates everywhere.
Fewer bugs: Reduces chances of making errors when updating code.
• Real-Life Example: Writing a recipe once and using it whenever you need to cook that dish instead of rewriting the recipe each time.
• Coding Scenario: Using a utility method for common operations.
public class StringUtils {
public static String capitalize(String input) {
return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
}
}
// Reuse capitalize method
String name = StringUtils.capitalize("hello");
Ref:https://medium.com/@kushanpeiris1118/most-common-interview-concept-what-are-solid-dry-and-acid-principles-937ca2945977
