Message passing between objects

T

Tod

I posted a question on this topic yesterday but used an example
specific to my application. But I wanted to re-post with a slightly
more abstract example and see what responses there are. My question:
How does ruby implement passing messages between objects.

An example might be a course registration system. In this system you
have student objects and a registration object. Your student object
can call the method student.register_for_course method. Now, before the
studen can register for the course we need to make sure the student has
met all the prerequisites. So ideally the student object would pass
the request to the registration object along with the name of the
course, the students id, etc. The registration object would call the
registration.prerequisite_met? method. Then it would return the
response back to the student object which could then procede with
either registering for the class or indicate that registration would
not be allowed.

Someone yesterday said you would create a superclass that both of these
objects would share and this superclass would handle communication
between the objects. I can see how that would work. But is that the
only way? Is there no way for ruby objects to communicate directly with
one another?

Thanks. TOD
 
L

Logan Capaldo

On Wed, Sep 20, 2006 at 10:05:12PM +0900, Tod wrote:
[snip question]
Was it really necessary to send the _exact_ same message twice, with a
slighty different Subject line?
 
T

Tod

Logan said:
On Wed, Sep 20, 2006 at 10:05:12PM +0900, Tod wrote:
[snip question]
Was it really necessary to send the _exact_ same message twice, with a
slighty different Subject line?

Operator error. Too many things going on at once.

I do apologize.

TOD
 
T

Tod

Paul said:
Tod wrote:

/ ...


No, in fact, there are any number of ways to do this. The question you
should be asking is which of the available methods meets your requirements
most efficiently.

Obviously, using your example of a student registration system, the
student's identity is the key to the relationship between objects.
Therefore a parent class containing everything that is known about the
student should deal with various aspects of the student -- registration,
arranging for classes, health care, grades, whatever.
cture is also
easy for the maintainer to understand and modify.
In the process of applying for a particular class for example, the object
responsible for requesting a particular course would pass this request to a
parent class that coordinates all activities for that particular student,
and that class on turn would communicate with a class responsible for
assigning courses university-wide.

The student's data is obviously self-contained, no peer-to-peer transactions
required. But the communication between the student's data and that
belonging to the university would be by way of another hierarchy of
classes, one in which the university's master record-keeping system would
be the parent of each student record.

My point is true peer-to-peer transactions without a parent class to
coordinate the activities are rare, and should be avoided if possible. Many
data relations are naturally hierarchical, and this archite

Thank you so much Paul for taking the time to lay that out for me. I
appreciate it. The reason for the persistent nature of my question
originates from a book I have on Java (I am not a Java programmer but
this book helps me object oriented concepts) in which there is a
sub-heading in a chapter: Message Passing Between Objects (hence my
post title). In this section it appears (to my hazy mind) that they
are describing a direct object to object interaction. But I may be
wrong.

Here is what it appears to be saying using the student example:

Student s = new Student()
Course c = new Course()

Course c recieves the message: c.register(s)

before Course c can register s it sends a message to Student s, perhaps
s.successfullyCompleted(c2) where c2 is some other course that is
required for the current registration request. Student s replies to
Course c with true. Now Course c can register Student s and confirms
the to whatever originated the request (a GUI perhaps) by returning
true.

To me that looks like two objects communicating directly with one
another. But I am way out of my league here. The book is "Beginning
Java Objects (2nd Ed) Jacquie Baker (2005 Apress).

But again, thank you for your post above. TOD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top