permaculture playing cards*
The moose likes Beginning Java and the fly likes HAS-A relationship? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HAS-A relationship?" Watch "HAS-A relationship?" New topic
Author

HAS-A relationship?

Akhash Ramamurthy
Greenhorn

Joined: Nov 02, 2009
Posts: 25
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?



Software Engg
Akhash
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Please do not post the same question more than once.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Nov 02, 2009
Posts: 25
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Jan 05, 2010
Posts: 64
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!


Don't walk as if you rule the world, walk as if you don't care who rules it...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Akhash Ramamurthy wrote:



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


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
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

Joined: Mar 17, 2010
Posts: 1051

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.


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:
Akhash Ramamurthy wrote:



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

Good Catch..Great..
Akhash Ramamurthy
Greenhorn

Joined: Nov 02, 2009
Posts: 25
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

Joined: Nov 02, 2009
Posts: 25
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

Joined: Nov 02, 2009
Posts: 25
My actual problem is how to relate these two classes in a UML diagram? Whats the relationship they share ?
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

UML:
Remote -> Light

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

Gary


star chaser..
Akhash Ramamurthy
Greenhorn

Joined: Nov 02, 2009
Posts: 25
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

Joined: Jan 05, 2010
Posts: 64
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

Joined: Jan 05, 2010
Posts: 64
I am not an avid UML user, but isn't "has-a" relationship denoted like this...
Remote<--Light...?

Akhash Ramamurthy
Greenhorn

Joined: Nov 02, 2009
Posts: 25
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

Joined: Oct 23, 2009
Posts: 150

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

Joined: Nov 02, 2009
Posts: 25
Gary,

Any suggestions for my question ?
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

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

Joined: Nov 02, 2009
Posts: 25
Thanks Gary. Thanks everyone for your replies...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HAS-A relationship?
 
Similar Threads
HAS-A relationship?
Enum Constructor
objects
SCJP Brainteaser (3)
Inherit private member