Question

I have, for example, a class Doctor who wants to use the services of a class Postman.

How does it look like in terms of code?

public class Doctor
{
    ...

    void doCheckup(Patient);
    void sendMail(){   // want to use services of a Postman here }
}

public class Postman
{
    void sendMail(...){ ... }
}

I might define Doctor::sendMail() as

void sendMail()
{
    Postman A = new Postman();
    A.sendMail(...);
}

Is that correct?

If it is, then why should a 'Doctor' create a 'Postman'? It sounds really far away from real life scenarios. Is that how messages are shared between objects?

Was it helpful?

Solution 2

If you're trying to make a "real world model" of things, then you would have a global PostOffice object, which would contain many Postmen.

The Doctor could be part of a DoctorOffice, or a Hospital

The Doctor would have a member variable pointing to HIS Postman

Doctor.Postman = aPostmanObject;

He would then let the postman do the work.

Doctor.Postman.SendMail();

Assuming you made "SendMail" a public method of Postman (which it would be, as he is doing this as a public service)

OTHER TIPS

A more idiomatic way would be to have something like:

void sendMail(Postman postman) {
   postman.sendMail(...);

Don't create the other object inside the first one.

On a sidenote, Doctor::sendMail is C++ syntax. In Java, there is no ::

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top