Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Relationship  RSS feed

 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a basic question on how to establish the object relationship in the following scenario.

"I have a cup that i have placed on a table".

I have a cup object and a table object ready. Now how do i relate both of them to depict the above scenario? :roll: Thinking for quite some time now.

Thanks
Venkatesh S
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In real software, the design depends on the application and the interactions between objects. If the table needs to query the objects it holds ("How heavy are you?") then the table object might contain a list of objects that are on it in a member variable. If the cup, on the other hand, needs to know something about the surface it sits on, then it will need a reference to the table so it can query the table. Or it might be that you need both. Without considering how the objects are to be used, there's no "right answer."
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venkatesh,

Among two relationship, is-a and has-a,
i think Table has a cup is best fit as table is a cup or cup is a table can never make a relationship.
Please correct me if i am wrong.
 
Rory Lynch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that the Table object should have a Cup instance variable which could be called myCup.
So that when the StarWars cup was placed on the table, you could say

Cup StarWars = new Cup;
this.myCup = StarWars;

now the Table 'HAS-A' cup, which is the Star Wars cup.

I am new to Java, only started learning in August so you must forgive me if I am way off.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks right on to me. I especially like that you made up an abstract Cup and a concrete StarWarsCup.

Let's step back and imagine somebody setting the table. They may place a bunch of cups:

Now we might imagine that Table has a collection of cups, maybe:

Set<Cup> cups = new HashSet();

Is that making sense so far?
 
Rory Lynch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I like it.

I just read about generics today, so it is good to see an example.
So, the HashSet will only accept new elements of type Cup which helps avoid runtime errors by catching them at compile time, right?

The cups could implement refillable,

java can be fun
 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Well it was not as simple as i thought. It needs loose coupling. Thats is today i can place a cup on the table. Tomorrow it might be placed on a different platform. So the place where you place the cup should be generic. I belive i am nearing the solution. Will post it once i am done.

Thanks
Venkatesh S
 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agile Software Development

I believe this should help

Thanks
Venkatesh S
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats is today i can place a cup on the table. Tomorrow it might be placed on a different platform.

Excellent! So table and the new platform have something in common: they can accept cups. That sounds like a decent Interface that any flat surface in the house might implement. Maybe even some part of your car.

Does your table hold 4 cups, or does it have 4 place settings that hold 1 each?

BTW: Real-world metaphors like this are fun, but rarely translate literally into software. Don't be too surprised when we just can't stretch them any further.
 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I agree with you Stan James.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Set<Cup> cups = new HashSet();

Is that making sense so far?


That wouldn't compile. As I'm sure you know, it should be: -

Set<Cup> cups = new HashSet<Cup>();

(I only pick you up on this because it's the beginner forum and genuine beginners might not have spotted what was wrong)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!