• Post Reply Bookmark Topic Watch Topic
  • New Topic

use X class object or LinkedList of X class as an instance variable of a Y class?  RSS feed

Peppy Florou
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all! This is Peppy, software translator/PM, new to Java and OOP in general.
I have only academic experience in procedural programming and my current
university course is OOP and Java.

I've read Head First Java up to chapter 10 - lllllove this book! I've also read
a bit of Thinking in Java. Still, I'm confused

I just delivered an assignment and I really die to have a clue on the following issue before I hear from my professor.
Apologies in advance about my dumb questions
Now, let me see if I can put my first query clearly...

We were asked to implement some basic functionality of a doctor-patients management system. Amongst others there were two classes involved namely Appointment and Patient. For these classes we were asked to define their variables, methods and implement some specific methods. We were also asked to implement the relationship of those two classes using a container structure, like a table etc. which we should incorporate to the Patient class. I used a LinkedList<Appointment> and successfully implemented all assignments questions - at least all .java files compiled successfully and I got the results I was supposed to get.

Though I do not understand why a container structure should be used as a variable to the Patient class and not let's say an Appointment object since If I'm not mistaken a HAS-A relationship exists between the two classes i.e.
class Patient
Appointment appointment;

hope I put this clearly,
thanks for your time
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

The little bit you showed was like this:

That's perfect if a patient has only zero or one appointment. But if you schedule three or four appointments ahead, you can't store more than one here. The collection is a good approach:

Why you put the collection on Patient instead of somewhere else has to do with Patient's responsibilities and the direction of navigation. With the code above, we apparently said "The Patient knows its own appointments" during design. Or we said "Given a patient we can find all of the appointments"

Let's say we don't trust patients with this information. Maybe we tried having them know about appointments but they tended to not show up. We could have a Scheduler that connects the Patient ot the Doctor or a Doctor that shows up wherever the patient is at the appointed time. These aren't realistic scenarios for doctors & patients but they might be for some other domain.

Does that answer the right question?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!