Message Passing
OOPs (Object-Oriented Programming) का सबसे बड़ा Advantage यह है, कि यह real-world objects को mimic करता है। जैसे – लोग एक – दूसरे से बात करते हैं, या काम करवाते हैं, वैसे ही OOPs में objects आपस में communicate करते हैं। इस communication को Message Passing कहते हैं।
Table of Contents
Toggle“Message Passing वह process है, जिसमें एक Sender process, Message (Data या Instruction) भेजता है और एक Receiver process उसे प्राप्त करता है। यह Process एक predefined protocol या mechanism के ज़रिए होता है।”
Message Passing Components –
- Sender Object : वह object जो message भेज रहा है।
- Receiver Object : वह object जिसे message भेजा जा रहा है।
- Message : वह method का नाम जिसे call किया जा रहा है।
- Parameters : वह information जो message के साथ भेजी जा रही है।
- Return Value: वह result जो receiver object से वापस मिलता है।
Why is message passing important?
(Message Passing क्यों ज़रूरी है?)
- Encapsulation : Message Passing ensure करता है, कि objects के internal data को सीधे access न किया जाए। हर object अपने data को protect करता है, और केवल methods के through ही communicate करता है।
- Loose Coupling : जब objects Message Passing के माध्यम से interact करते हैं, तो वे एक-दूसरे पर कम निर्भर रहते हैं। इससे system maintain और update करना आसान हो जाता है।
- Modularity : हर object independent module की तरह काम करता है, जिसकी अपनी responsibilities होती हैं। यह code को organized बनाता है।
- Clarity : method calls जैसे message.send() या user.placeOrder() code को readable और समझने में आसान बनाते हैं।
Types of Message Passing (प्रकार)
1. Synchronous Message Passing
Synchronous message passing में sender object message भेजता है, और receiver object का response आने तक wait करता है।
Sender तब तक आगे नहीं बढ़ता जब तक receiver का response नहीं आता।
Advantages (फायदे) –
- Easy to Implement : समझना और implement करना आसान है।
- Immediate Result : Communication का result तुरंत मिलता है।
- Simple Error Handling : Errors आसानी से detect और handle किए जा सकते हैं।
Disadvantages (नुकसान) –
- Waiting Required : Sender को response आने तक wait करना पड़ता है, program slow हो सकता है।
- Tight Coupling : Sender और receiver के बीच high dependency होती है।
- Deadlock Risk : अगर receiver busy या unavailable है, तो system block हो सकता है।
2. Asynchronous Message Passing
Asynchronous message passing में sender object message भेजने के बाद response का wait नहीं करता।
Sender आगे बढ़ सकता है, और receiver जब ready हो, तब message process करता है।
Advantages –
- Fast & Efficient : Sender जल्दी execute होता है।
- Loose Coupling : Objects independent रहते हैं।
- Large System Friendly : Multiple objects simultaneously communicate कर सकते हैं।
Disadvantages –
- Error Handling Difficult : Errors detect करना कठिन हैं।
- Uncertain Response Timing : Response कब आएगा पता नहीं होता
- Complex Implementation : Design और coding challenging हो सकती है।
Advantages (लाभ)
- Encapsulation Support
- Objects अपने internal data को hide रखते हैं, और केवल methods के माध्यम से communicate करते हैं।
- Data को direct access करने से रोकता है
- Loose Coupling
- Objects केवल messages के माध्यम से interact करते हैं।
- Reusability
- एक object का method आसानी से reuse किया जा सकता है, क्योंकि message passing standardized है।
- Flexibility and Maintainability
- System को modify या extend करना आसान होता है।
- New messages और objects add करना आसान है।
- Real-world Modeling
- Objects वास्तविक-world entities की तरह interact करते हैं, programs आसानी से understandable होते हैं।
Disadvantages (हानियाँ)
- Overhead
- Message send और receive करने में extra processing time लगता है।
- Slower Performance
- Especially synchronous message passing में sender को response का wait करना पड़ता है।
- Complexity
- Asynchronous message passing और multiple objects communication management मुश्किल हो सकती है।
- Debugging Difficulty
- Messages के flow को track करना मुश्किल हो सकता है, especially large systems में।




