Hi all,
public class Elevator ()
{
Button firstFloorbutton = ButtonFactory.getButtonInstance(this, 1);
Button secondFloorbutton = ButtonFactory.getButtonInstance(this, 2);
Button thirdFloorbutton = ButtonFactory.getButtonInstance(this, 3);
Button fourthFloorbutton = ButtonFactory.getButtonInstance(this, 4);
Fan fan1 = FanFactory.getFanInstance(this);
Light light1 = LightFactory.getInstance(this);
private void goUp()
{
.....
}
private void goDown()
{
......
}
.............
}
============================
public class Button()
{
Elevator E;
int floor;
public button (Elevator E, int floor )
{
this.E = E;
this.floor = floor;
}
public void buttonPressed()
{
//logic to determine which floor the elevator is currently at and determine whether to go up or down to reach "this.floor"
E.goUp(); // if need to go up
else
E.goDown() // if need to go down
}
}
==========================
public class ButtonFactory()
{
public Button getButtonInstance(Elevator E, int floor)
{
Button b =new Button(E, floor);
return b;
}
}
==================
public class FanFactory(){ .................}
=====================
public class LightFactory() { ........... }
==========================
What kind of relationship exist between the Elevator and Button class?
According to Kathy and Bert (SCJP) page 92 : HAS-A relationship are based on usage rather than inheritance. In other words, class A HAS-A B if code in class A has a reference to an instance of class B.
In my example Elevator class code have a reference to a instance of Button and Button have a reference to instance of Elevator class.
Can anyone please clarify on this.