Single Responsibility, OpenClosed, Liskov Substitution, Interface Segregation, Dependency Inversion In Hindi

Single Responsibility, OpenClosed, Liskov Substitution, Interface Segregation, Dependency Inversion In Hindi

जावा प्रोग्रामिंग लैंग्वेज में, इन पाँच इम्पोर्टेन्ट सिंगल रिस्पॉन्सिबिलिटी, ओपन/क्लोज्ड, लिस्कोव सब्स्टिट्यूशन, इंटरफेस सेग्रीगेशन, डिपेंडेंसी इनवर्जन प्रिंसिपल्स को SOLID प्रिंसिपल्स के रूप में जाना जाता है। ये जावा डेवलपर को एक ऐसा कस्टम यूजर डिफाइन प्रोग्राम सोर्स कोड क्रिएट करने में हेल्प करते हैं. जिसे हर जावा सॉफ्टवेयर डेवलपर के लिए मेंटेन करना, इनक्रीस करना, इजीली टेस्ट और डिबग करना और मॉडुलर प्रोग्रामिंग कांसेप्ट से मल्टीप्ल टाइम दोबारा यूज़ करना आसान होता है।

Single Responsibility, OpenClosed, Liskov Substitution, Interface Segregation, Dependency Inversion In Hindi

S — Single Responsibility Principle (SRP) concept in Java.

जावा प्रोग्राम में इम्पोर्टेन्ट सिंगल रिस्पॉन्सिबिलिटी कांसेप्ट में एक यूजर डिफाइन क्लास में किसी प्रकार के कस्टम मैन्युअल मॉडिफिकेशन करने का सिर्फ़ एक इम्पोर्टेन्ट कारण होना चाहिए।

सामान्य अर्थ में, एक यूजर डिफाइन क्लास की कुछ रेस्पॉन्सिबिलिटीज़ होनी चाहिए।

Example of S — Bad the Single Responsibility Principle.

class Employee {

void calculateSalary() {

// this class used to count employee salary

}

void saveToDatabase() {

// this class used to save employee salary details

}

void generateReport() {

// this class used to generate or display employee salary report

}

}

Here are the three main roles of S — Single Responsibility Principle (SRP).

  • एम्प्लॉई की सैलरी कैलकुलेशन करना।
  • एम्प्लॉई की सैलरी डेटाबेस को स्टोरेज करना।
  • एम्प्लॉई सैलरी डिटेल रिपोर्ट को जनरेट करना।

Example of Improved S —  the Single Responsibility Principle.

class SalaryCalculator {

void calculateSalary() {

// Here we set code for count emloyee salary

}

}

class EmployeeRepository {

void save() {

// Here we set code for save employee salary database info

}

}

class ReportGenerator {

void generate() {

// Here we set code for display or generate employee salary report

}

}

Complete Example of S — Single Responsibility Principle.

    class EmpSalaryManagement {

        // Here we set Responsibility 1: count employee salary

        void calculateSalary(double empSalary) {

            double hra = empSalary * 0.31;

            double da = empSalary * 0.17;

            double ta = empSalary * 0.23;

            double completeSalary = empSalary + hra + da + ta;

            System.out.println(“Total Salary of Employee Is -: ” + completeSalary);

        }

    }

    class EmployeeDataRecord {

        // Here we set Responsibility 2: Save employee salary data or details

        void save(String emp_name, int id) {

            System.out.println(“-:- Employee Record save to database -:-“);

            System.out.println(“Employee ID -: ” + id);

            System.out.println(“Employee Name -: ” + emp_name);

        }

    }

    class EmpReportCreator {

        // Here we set Responsibility 3: Create employee total salary report

        void generate(String emp_name, int emp_id, double totalsalary) {

            System.out.println(“\n—– Employee Salary Details —–“);

            System.out.println(“Employee ID Is -: ” + emp_id);

            System.out.println(“Employee Name Is -: ” + emp_name);

            System.out.println(“Basic Salary Is -: ” + totalsalary);

            System.out.println(“———#################——————“);

        }

    }

    // Here we set Main class

    public class Main {

        public static void main(String[] args) {

            String emp_name = “Siddhi Deora”;

            int emp_id = 1109;

            double empSalary = 90000;

            // Here we create employee class objects

            EmpSalaryManagement salmanagement = new EmpSalaryManagement();

            EmployeeDataRecord emprecord = new EmployeeDataRecord();

            EmpReportCreator empreport = new EmpReportCreator();

            // Here we manage or Calculate employee salary

            salmanagement.calculateSalary(empSalary);

            // Here we save employee details information

            emprecord.save(emp_name, emp_id);

            // Here we generate employee salary report

            empreport.generate(emp_name, emp_id, empSalary);

        }

    }

The main idea of ​​the S — Single Responsibility Principle in Java.

  • एक यूजर डिफाइन कस्टम जावा क्लास → और एक क्लास रेस्पॉन्सिबिलिटीज़

O — Open/Closed Principle (OCP) concept in Java.

जावा प्रोग्राम में ओपन/क्लोज्ड कांसेप्ट मेथड एक यूजर डिफाइन क्लास में सॉफ्टवेयर एंटिटीज़ एक्सटेंशन के लिए ओपन होनी चाहिए। लेकिन सामान्य जावा यूजर के लिए किसी भी प्रकार के कस्टम मॉडिफिकेशन अपडेट के लिए क्लोज्ड होनी चाहिए।

जिसमे जावा डेवलपर को मौजूदा, प्रोग्राम टेस्टेड सक्सेस सोर्स कोड को मॉडिफाई किये बिना नई इम्पोर्टेन्ट कस्टम क्लास फंक्शनैलिटी को मैन्युअल ऐड करने के कण्ट्रोल होने चाहिए।

Example of the Bad O — Open/Closed Principle (OCP).

class Payment {

void pay(String type) {

if (type.equals(“UPI Method”)) {

// here we can create code for UPI payment method

}

else if (type.equals(“Credit CARD”)) {

// here we can create code for Credit Card payment method

}

}

}

यहाँ इस इनकरेक्ट ओपन/क्लोज्ड कांसेप्ट सोर्स कोड मेथड में हर बार जब हम कोई नया पेमेंट मेथड को ऐड करते हैं, तो जावा डेवलपर को पहले Payment को मॉडिफाई करना पड़ता है।

Example of the Better O — Open/Closed Principle (OCP).

interface Payment {

void pay();

}

class UpiPayment implements Payment {

public void pay() {

System.out.println(“We use UPI Payment Method”);

}

}

class CardPayment implements Payment {

public void pay() {

System.out.println(“We use Card Payment Method”);

}

}

Then, the Java developer can add to it.

class CashPayment implements Payment {

public void pay() {

System.out.println(“We use Cash Payment Method”);

}

}

Complete Example of O — Open/Closed Principle (OCP) Principle.

interface PaymentGateway {

    void paymethod();

}

// Payment Gateway Method 1: set UPI Payment

class UpiPayment implements PaymentGateway {

    public void paymethod() {

        System.out.println(“———–List of Available Payment Gateway Method—————–“);

        System.out.println(“You Can Use UPI Payment Gateway Method”);

    }

}

// Payment Gateway Method 2: set Card Payment

class CardPayment implements PaymentGateway {

    public void paymethod() {

        System.out.println(“You Can Use Debit/Credit Card Payment Gateway Method”);

    }

}

// Payment Gateway Method 3: set Cash Payment

class CashPayment implements PaymentGateway {

    public void paymethod() {

        System.out.println(“You Can Use Cash Payment Method”);

    }

}

// Payment Gateway Method 4: set Onlline Payment

class OnlineMethod implements PaymentGateway {

    public void paymethod() {

        System.out.println(“You Can Use Online Payment Method”);

        System.out.println(“—————-######################—————-“);

    }

}

// Here we define a Main paymet gateway class

public class Main {

    public static void main(String[] args) {

        // here we create paymetgateway class objects

        PaymentGateway upi = new UpiPayment();

        PaymentGateway card = new CardPayment();

        PaymentGateway cash = new CashPayment();

        PaymentGateway online = new OnlineMethod();

        // here we execute all user define Payment Gateway methods interface

        upi.paymethod();

        card.paymethod();

        cash.paymethod();

        online.paymethod();

    }

}

ओपन/क्लोज्ड जावा प्रिंसिपल में मौजूदा जावा क्लास को मॉडिफाई किये बिना आप अन्य क्लास में चेंज कर सकते है।

The main idea of ​​the O — Open/Closed Principle (OCP) Principle in Java.

  • ओपन/क्लोज्ड जावा प्रिंसिपल से डेवलपर मौजूदा प्रोग्राम सोर्स कोड को मॉडिफाई किये बिना मौजूदा प्रोग्राम फंक्शन फीचर्स को इनक्रीस करने में हेल्प करता है।

L — Liskov Substitution Principle (LSP) concept in Java.

जावा प्रोग्राम में लिस्कोव सब्स्टिट्यूशन कांसेप्ट मेथड एक यूजर डिफाइन क्लास में चाइल्ड क्लास के ऑब्जेक्ट्स को प्रोग्राम में मैन्युअल ब्रेक किये बिना पैरेंट रुट या बेस क्लास के किसी भी यूजर डिफाइन क्लास ऑब्जेक्ट्स से मॉडिफाई किया जा सकने वाला होना चाहिए।

सामान्य भाषा में, यदि Employee क्लास, Company क्लास में सबक्लास के रूप में डिफाइन है, तो जावा यूजर को Employee क्लास का यूज़ वहाँ कर पाना चाहिए, जहाँ Company क्लास में इसकी पॉसिबिलिटी हो।

Example of a bad Liskov Substitution Concept.

class Bird {

void canfly() {

System.out.println(“Bird Can Flying”);

}

}

class Sparrow extends Bird {

// Source code for Can Birds Fly

}

class Penguin extends Bird {

void canfly() {

throw new UnsupportedOperationException();

Here we create a Bird class called Penguin with exception behavior.

}

}

याद रहे, यहाँ Bird पैरेंट क्लास में Penguin एक बर्ड है, लेकिन अधिक वजन होने के कारण यह Bird पैरेंट क्लास से उम्मीद किए गए fly() क्लास बिहेवियर को पूरा नहीं कर सकता है।

Example of a Better Liskov Substitution Concept.

क्षमताओं को अलग करें।

class Bird {

void eat() {

System.out.println(“What Burd Eating”);

}

}

interface FlyingBird {

void fly();

}

class Sparrow extends Bird implements FlyingBird {

public void fly() {

System.out.println(“Can Bird Flying”);

}

}

class Penguin extends Bird {

// Unable to fly() required

}

Complete Example of Liskov Substitution Principal Concept.

class Bird {

    // Here we set a common class behavior of all birds subclass

    void eating() {

        System.out.println(“This Bird is Eat Food”);

    }

}

// Here we set a Interface for birds class that can flying

interface FlyingBird {

    void flying();

}

// Here we set a Parrot inherits Bird and implements FlyingBird

class Parrot extends Bird implements FlyingBird {

    public void flying() {

        System.out.println(“Parrot Can Flying”);

    }

}

// Here we set a Pigeon inherits Bird and implements FlyingBird

class Pigeon extends Bird implements FlyingBird {

    public void flying() {

        System.out.println(“Pigeon Is Also Can Flying”);

    }

}

// Here Penguin inherits Bird category but due to overweight it does not implement FlyingBird capabilities

class Penguin extends Bird {

    // Penguin is burd but it cannot flying

}

// here we define a Main class

public class Main {

    public static void main(String[] args) {

        // here we ceate a Parrot subclass object

        Parrot parrot = new Parrot();

        System.out.println(“—– Parrot Bird Details —–“);

        parrot.eating();

        parrot.flying();

          // here we create a Pigeon subclass object

        Pigeon pigeon = new Pigeon();

        System.out.println(“\n—– Pigeon Bird Details —–“);

        pigeon.eating();

        pigeon.flying();

        // here we create a Penguin object

        Penguin penguin = new Penguin();

        System.out.println(“\n—– Penguin Details —–“);

        penguin.eating();

    }

}

The main idea of ​​the Liskov Substitution Concept Principle in Java.

  • यहाँ लिस्कोव सब्स्टिट्यूशन जावा कांसेप्ट में यूजर डिफाइन डिक्लेअर सभी चाइल्ड क्लास को अपने पैरेंट या रुट क्लास से प्रॉपर आर्डर में बिहेव करना चाहिए।

I — Interface Segregation Principle (ISP) concept in Java.

जावा प्रोग्राम में इंटरफेस सेग्रीगेशन कांसेप्ट मेथड किसी क्लास को ऐसे मेथड इम्प्लीमेंट करने के लिए फाॅर्स नहीं किया जाना चाहिए, जिनकी उसे ज़रूरत नहीं है।

एक क्लास में बहुत बड़ा इंटरफ़ेस क्रिएट करने के बदले, स्माल, स्पेशल क्लास इंटरफ़ेस क्रिएट करे।

Example of a bad Interface Segregation Concept.

interface Worker {

void work();

void eat();

}

Here, let’s assume a robot implements it.

class Robot implements Worker {

public void work() {

System.out.println(“It’s Working”);

}

public void eat() {

// A robot, a machine, does not eat food.

}

}

यहाँ जावा यूजर के द्वारा रोबोट एक मशीन को एक गैर-ज़रूरी टास्क या मेथड को इम्प्लीमेंट करने के लिए मजबूर किया जाता है।

Example of a Better Interface Segregation Concept.

interface Workable {

void work();

}

interface Eatable {

void eat();

}

Here, the class Human implements Workable, Eatable {

public void work() {

System.out.println(“It’s Working”);

}

public void eat() {

System.out.println(“It’s Eating”);

}

}

class Robot implements Workable {

public void work() {

System.out.println(“It’s Working”);

}

}

Complete Example of Interface Segregation Principle (ISP) concept Principle.

// Here we set Interface 1: Defines all working behaviour

interface Workable {

    void working();

}

// Here we set Interface 2: Defines all class eating behaviour

interface Eatable {

    void eating();

}

// Here Man class implements both interfaces

class Man implements Workable, Eatable {

    public void working() {

        System.out.println(“Man is Working”);

    }

    public void eating() {

        System.out.println(“Man is Eating”);

    }

}

// Here the Robot class implements only with Workable interface not to other eating interface

class Robot implements Workable {

    public void working() {

        System.out.println(“Robot is Working”);

    }

}

// Here we set Main class

public class Main {

    public static void main(String[] args) {

        // Here we create Man class object

        Man man = new Man();

        System.out.println(“—– Man Activities —–“);

        man.working();

        man.eating();

        // Here we create Robot class object

        Robot robot = new Robot();

        System.out.println(“\n—– Robot Activities —–“);

        robot.working();

    }

}

The main idea of ​​the Interface Segregation Principle (ISP) concept Principle in Java.

  • यहाँ इंटरफेस सेग्रीगेशन एक्साम्प्ल में बड़े क्लास इंटरफ़ेस के बदले स्माल, फ़ोकस्ड क्लास इंटरफ़ेस ऑब्जेक्ट को प्रायोरिटी प्रोवाइड की जाती है।

D — Dependency Inversion Principle (DIP) concept in Java.

जावा प्रोग्राम में डिपेंडेंसी इनवर्जन प्रिंसिपल्स कांसेप्ट मेथड को किसी क्लास में हाई-लेवल मॉड्यूल से डायरेक्ट लो-लेवल मॉड्यूल क्लास पर कम्पलीट डिपेंड नहीं होने चाहिए। इन दोनों क्लास मॉड्यूल को एब्स्ट्रैक्शन क्लास मेथड पर डिपेंड होना चाहिए।

सामान्य भाषा में, एक यूजर डिफाइन क्लास को किसी स्पेशल क्लास इम्प्लीमेंटेशन से टाइटली कपल न करें।

Example of a bad Dependency Inversion Principal Concept.

class MySQLDatabase {

void save() {

System.out.println(“Data Save  to MySQL”);

}

}

class UserService {

private MySQLDatabase database = new MySQLDatabase();

void saveUser() {

database.save();

}

}

यहाँ इस एक्साम्प्ल में UserService, MySQLDatabase इंटरफ़ेस से टाइटली कपल्ड या डिपेंड है।

यदि यहाँ यूजर को PostgreSQL डेटाबेस चाहिए, तो यूजर को UserService को मैन्युअल मॉडिफाई करना होगा।

Example of a Better Dependency Inversion Principal Concept.

Create an abstraction class method.

interface Database {

void save();

}

Implement this in a class.

class MySQLDatabase implements Database {

public void save() {

System.out.println(“Save data to MySQL”);

}

}

class PostgreSQLDatabase implements Database {

public void save() {

System.out.println(“Save data to PostgreSQL”);

}

}

Now let’s use a dependency injection method.

class UserService {

private Database database;

UserService(Database database) {

this.database = database;

}

void saveUser() {

database.save();

}

}

Final use.

UserService service =

new UserService(new MySQLDatabase());

service.saveUser();

Now we can easily switch:

UserService service =

new UserService(new PostgreSQLDatabase());

Complete Example of Dependency Inversion Principle (DIP) concept Principle.

// Here we set Interface: Database abstraction class method

interface Database {

    void save();

}

// Here MySQL Database implementation method

class MySQLDatabase implements Database {

    public void save() {

        System.out.println(“Save data to MySQL Database Software”);

    }

}

// Here PostgreSQL Database implementation method

class PostgreSQLDatabase implements Database {

    public void save() {

        System.out.println(“Save data to PostgreSQL Database Software”);

    }

}

// Here database UserService totally depends on above created Database interface

class UserService {

    private Database database;

    // Here we define Constructor Dependency Injection method

    UserService(Database database) {

        this.database = database;

    }

    void saveUser() {

        database.save();

    }

}

// Here we define Main class

public class Main {

    public static void main(String[] args) {

        // Here we Using MySQL Database

        System.out.println(“—– MySQL Database Update —–“);

        UserService service1 =

            new UserService(new MySQLDatabase());

        service1.saveUser();

        // Here we Using PostgreSQL Database

        System.out.println(“\n—– PostgreSQL Database Update —–“);

        UserService service2 =

            new UserService(new PostgreSQLDatabase());

        service2.saveUser();

    }

}

The main idea of ​​the Dependency Inversion Principle (DIP) concept Principle in Java.

  • यहाँ डिपेंडेंसी इनवर्जन प्रिंसिपल्स एक्साम्प्ल में डेटाबेस यूजर कंक्रीट क्लास पर डिपेंड नहीं रहे, बल्कि इंटरफेस/एब्स्ट्रैक्शन क्लास पर टोटली डिपेंड रहें।

Main Use and Description of All 5 Solid Principal in One Table

#PrincipleDetail Principal NameEasy Implementation With
1.SSingle ResponsibilitySingle responsibility principal used with one class → one responsibility only method
2.OOpen/ClosedOpen/closed principal used to extend without modifying its existing source code
3.LLiskov SubstitutionLiskov substitution principal used with child class should be usable as its parent class
4.IInterface SegregationInterface segregation principal keeps interfaces small and specific class element
5.DDependency InversionDependency inversion principal depends on abstractions class method, not concrete classes

Conclusion of Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion.

जावा में इन सॉलिड 5 प्रिंसिपल को याद रखने का एक आसान तरीका है।

  • S → सिंगल रिस्पॉन्सिबिलिटी को सिंगल जॉब क्लास इंटरफ़ेस मेथड में यूज़ करे.
  • O → ओपन/क्लोज्ड मेथड को ओपन एक्सटेंशन के लिए यूज़ करे.
  • L → लिस्कोव सब्स्टिट्यूशन मेथड को पैरेंट की तरह क्लास में यूज़ करे.
  • I → इंटरफेस सेग्रीगेशन मेथड का इंटरफेस छोटा होने में यूज़ करे.
  • D → डिपेंडेंसी इनवर्जन प्रिंसिपल्स क्लास में एब्स्ट्रैक्शन कांसेप्ट पर टोटली डिपेंड रहें

ये SOLID प्रिंसिपल्स जावा, स्प्रिंग बूट और सिस्टम/डिज़ाइन इंटरव्यू में एसेंशियल कांसेप्ट हैं, क्योंकि ये सभी प्रिंसिपल जावा यूजर को इंडीकेट करते हैं कि जावा यूजर एक क्लास, इंटरफेस और डिपेंडेंसी को जिस तरह से क्रिएट करते हैं, वह क्यों बनाते हैं।

Leave a Reply