Strategy, Observer, Decorator Java In Hindi

Strategy, Observer, Decorator Java In Hindi

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

Strategy, Observer, Decorator Java In Hindi

Strategy Pattern Behavioral/Structural concept in Java.

जावा प्रोगाम में स्ट्रैटेजी पैटर्न जावा डेवलपर को मल्टीप्ल क्लास एल्गोरिदम/बिहेवियर को मैन्युअल डिफाइन करने और रनटाइम पर इनमे से एक को सलेक्ट करने के फीचर्स प्रोवाइड करता है।

Example of different Strategy Pattern Behavioral payment methods.

interface PaymentGateway {

    void pay();

}

class CreditCardGateway implements PaymentGateway {

    @Override

    public void pay() {

        System.out.println(“Pay with a credit card method”);

    }

}

class UpiGateway implements PaymentGateway {

    @Override

    public void pay() {

        System.out.println(“Pay with UPI method”);

    }

}

class PaymentMethod {

    private PaymentGateway gateway;

    public PaymentMethod(PaymentGateway gateway) {

        this.gateway = gateway;

    }

    public void makePaymentMethod() {

        gateway.pay();

    }

    public void setGateway(PaymentGateway gateway) {

        this.gateway = gateway;

    }

}

public class Main {

    public static void main(String[] args) {

        // here we payment using Credit Card

        PaymentMethod paymentmethod =

                new PaymentMethod(new CreditCardGateway());

        paymentmethod.makePaymentMethod();

        // here we change gateway to UPI

        paymentmethod.setGateway(new UpiGateway());

        paymentmethod.makePaymentMethod();

    }

}

its output.

Pay with a credit card method

Pay with UPI method

Here’s the Strategy Pattern Behavioral main idea in this example.

  • एक ही काम → अलग-अलग एल्गोरिदम पैटर्न → प्रोग्राम रनटाइम पर सलेक्ट करे।
  • Real-world examples – पेमेंट के तरीके, सॉर्टिंग एल्गोरिदम, नेविगेशन रूट।

Observer Pattern Behavioral/Structural concept in Java.

जावा प्रोगाम में ऑब्ज़र्वर पैटर्न एक-से-कई ऑब्जेक्ट रिलेशनशिप को बिल्ड या क्रिएट करता है।

जब यूजर डिफाइन एक क्लास में कोई ऑब्जेक्ट मॉडिफाई होता है, तो उसके सब्सक्राइब किए गए सभी क्लास ऑब्जेक्ट को अपने आप यह पता हो जाता है।

Example of YouTube Subscriber in the Observer Pattern Behavioral/Structural.

import java.util.ArrayList;

import java.util.List;

// here we create a observer Interface

interface Observer {

    void update(String info);

}

// here we create class Observer

class Subscriber implements Observer {

    private String subscriber_name;

    public Subscriber(String subscriber_name) {

        this.subscriber_name = subscriber_name;

    }

    @Override

    public void update(String info) {

        System.out.println(subscriber_name + ” video updates -: ” + info);

    }

}

// here we create a channel Subject

class YouTubeChannel {

    private List<Observer> subscribers = new ArrayList<>();

    public void subscribe(Observer observer) {

        subscribers.add(observer);

    }

    public void uploadVideo(String video) {

        System.out.println(“New channel video uploaded -: ” + video);

        for (Observer observer : subscribers) {

            observer.update(video);

        }

    }

}

// here we create a Main Class

public class Main {

    public static void main(String[] args) {

        YouTubeChannel channel = new YouTubeChannel();

        // here we create a youtube channel Subscribe users list

        channel.subscribe(new Subscriber(“Bhavishi”));

        channel.subscribe(new Subscriber(“Siddhi”));

        channel.subscribe(new Subscriber(“Harry”));

        // here we Upload a new channel video

        channel.uploadVideo(“Strategy, Observer, Decorator tutorial in java”);

    }

}

its output.

New channel video uploaded -: Strategy, Observer, Decorator tutorial in java

Bhavishi video updates -: Strategy, Observer, Decorator tutorial in java

Siddhi video updates -: Strategy, Observer, Decorator tutorial in java

Harry video updates -: Strategy, Observer, Decorator tutorial in java

Here’s the Observer Pattern Behavioral main idea in this example.

  • जब एक क्लास ऑब्जेक्ट बदलता है → तब यह उस पर निर्भर कई क्लास ऑब्जेक्ट एलिमेंट को एक साथ नोटिफाई करता है।
  • Real-world examples → यह आपके लिए YouTube सब्सक्रिप्शन डिटेल इन्फोर्मशन, लाइव स्टॉक-प्राइस अपडेट अलर्ट में, इवेंट लिसनर कांसेप्ट, आदि नोटिफिकेशंस के इसे यूज़ किया जा सकता है।

Decorator Pattern Behavioral/Structural concept in Java.

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

Example of IndianThaliFood Decorator Pattern Behavioral.

// Here we create a IndianThaliFood Interface

interface IndianThaliFood {

    String getDescription();

    double getCost();

}

// Here we define a basic IndianThaliFood item

class IndianThali implements IndianThaliFood {

    @Override

    public String getDescription() {

        return “Traditional IndianThaliFood”;

    }

    @Override

    public double getCost() {

        return 899;

    }

}

// Here we create a Decorator method

class RayataDecorator implements IndianThaliFood {

    private IndianThaliFood indianthalifood;

    public RayataDecorator(IndianThaliFood indianthalifood) {

        this.indianthalifood = indianthalifood;

    }

    @Override

    public String getDescription() {

        return indianthalifood.getDescription() + ” + Curd Rayata”;

    }

    @Override

    public double getCost() {

        return indianthalifood.getCost() + 100;

    }

}

// Here we create a main Class

public class Main {

    public static void main(String[] args) {

        // Here we create a Traditional IndianThaliFood

        IndianThaliFood indianthalifood = new IndianThali();

        // Here we create a curd rayata add item detail

        indianthalifood = new RayataDecorator(indianthalifood);

        // Here we display indian thali details

        System.out.println(“Indian Thali Food -: ” + indianthalifood.getDescription());

        System.out.println(“Total Cost of Thali In Rs -: ” + indianthalifood.getCost());

    }

}

its output.

Indian Thali Food -: Traditional IndianThaliFood + Curd Rayata

Total Cost of Thali In Rs -: 999.0

Java developers can add decorators to their programs like this.

IndianThali

           ↓

+ Curd Rayata

           ↓

+ Desi Butter Milk

           ↓

+ Papad

          ↓

+ Salad

Here’s the Decorator Pattern Behavioral main idea in this example.

यहाँ इस एक्साम्प्ल में, क्लास ऑब्जेक्ट की ओरिजिनल क्लास को मॉडिफाई किये बिना उसमें जरूरत के अनुसार मल्टीप्ल नई क्लास फीचर्स और फंक्शनैलिटी को ऐड कर सकते है।

Main Difference Among Strategy, Observer, Decorator Method in Java

#Pattern BehaviourPurpose of Pattern BehaviourApply Method
1.Strategy patternStrategy pattern used to change an algorithm/behavior of object of class with single choiceSelect choose only 1 behavior
2.Observer patternObserver pattern informs or notifies all dependent objects in single or group classAction one → many class object
3.Decorator patternDecorator pattern used to add one or more multiple class functionality dynamically in existing group elementHere it wraps a class object

Conclusion of Strategy, Observer, and Decorator in Java.

  • Strategy Pattern → किसी जावा प्रोग्राम में कुछ करने का एक मेथड तरीके को सलेक्ट करे.
  • Observer Pattern → किसी जावा प्रोग्राम में जब कुछ एक्शन या इवेंट ट्रिगर हो तो एक साथ सबको इन्फॉर्म या नोटिफिकेशन प्रोवाइड करे.
  • Decorator Pattern → किसी जावा प्रोग्राम में मौजूदा ग्रुप ऑब्जेक्ट में नए फ़ीचर को ऐड करने के लिए क्लास ऑब्जेक्ट को अपने हिसाब से रैप करते रहे.

Leave a Reply