This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HAS-A relationship?

 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the relationship between the LIGHT and REMOTE class?





Whats the relationship between the LIGHT and REMOTE class? (interms of association or composition)

I dont think Remote Has-A a light is valid relationship.... But in the real-world Light Has-A Remote seems to be correct for me.... Whats your opinion?


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64694
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post the same question more than once.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Please do not post the same question more than once.
Agree. Since the other posting was on somebody else's thread, that is regarded as hi-jacking the old thread. I have obscured the other post so you won't get any replies there.
 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for posting the question twice. Thought it might go unnoticed if my question is under a old thread... So created a new thread.
Anyways can I get an answer for my question ?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted
It was better to create a new thread for a new question.

I think you are correct: remote has-a light. Whether that has anything to do with the real world is not important. Your code creates a situation where a remote has a light, and if the real world is different, well that's the real world's problem .
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Akhash,

What you are thinking is absolutely correct, and it should be that way and it can.

I don't think switchOn() or switchOff() is the behavior of light, it is the function of a remote. The light simply responds to these commands. You could think on those lines...

Campbell Ritchie wrote: ..and if the real world is different, well that's the real world's problem.

Javattidude!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akhash Ramamurthy wrote:



There is a syntax error in the coding. Nobody noticed?
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote: There is a syntax error in the coding. Nobody noticed?


Whoops!!...Think I got too excited about leaving for home...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
donot compare this code with a real world..
you can even make a class Animal which has all the birds



but i can see that your code has class remote which call light class methods to check whether the button is pressed or not.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
Akhash Ramamurthy wrote:



There is a syntax error in the coding. Nobody noticed?

Good Catch..Great..
 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kuchekar wrote:
Abimaran Kugathasan wrote: There is a syntax error in the coding. Nobody noticed?


Whoops!!...Think I got too excited about leaving for home...


Thanks. I have got it rectified.
 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kuchekar wrote:Hi Akhash,

What you are thinking is absolutely correct, and it should be that way and it can.

I don't think switchOn() or switchOff() is the behavior of light, it is the function of a remote. The light simply responds to these commands. You could think on those lines...

Campbell Ritchie wrote: ..and if the real world is different, well that's the real world's problem.

Javattidude!


I actually tried to create a loosely coupled design. That made me think ON and OFF is something that the light should do and not the remote... Later if the requirement changes and if I introduce a dimmer behavior for the light I can still use the same toggleSwitchPressed() function in the Remote to dim or brighten the light...
 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My actual problem is how to relate these two classes in a UML diagram? Whats the relationship they share ?
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UML:
Remote -> Light

has-a/association relationship can be shown on UML diagram as an arrow.

Gary
 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Ba wrote:UML:
Remote -> Light

has-a/association relationship can be shown on UML diagram as an arrow.

Gary


Thanks Gary.
How you say Remote --> Light (Has-A) relation?
Can you please explain me.... I have an interesting scenario based on your answer.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what Gary has said above is perfect. If you are still fighting for a real world solution and to make it loosely coupled create an Interface, say Transponder.

So something like:

Now,

Now use this Interface reference in your class Remote. You could also add some more Transponders by implementing this interface. Now your Remote "has-a" Transponder.

There is much more you could do to the above code to make it close to real world. You could add another abstract level between the interface and your class. But this one is simple enough.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not an avid UML user, but isn't "has-a" relationship denoted like this...
Remote<--Light...?

 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I am taking this very deep..






Whats the relationship between the Elevator class and the Button class ?
I kind of confused since Elevator has Button members and vice versa....

I didnt use a IDE to type my code.... so if any typos please dont pounce on me... :-)
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not an avid UML user, but isn't "has-a" relationship denoted like this...
Remote<--Light...?


The arrow should point the other way around. As far as straight line and broken arrow, I know straight line has stronger relationship than dashes. (maybe this is a C++ thing).

Gary
 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary,

Any suggestions for my question ?
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the relationship between the Elevator class and the Button class ?


It is called Aggregation. Read up on those keyword so you can get a better grasp out of it.
I think it is denoted as

Elevator 1 -> * Buttons

Garry
 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gary. Thanks everyone for your replies...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic