I have a pretty simple scenario here and it works, but my instincts tell me I'm making a mistake and would like to run it by some better minds.
Here I have a parent class that instantiates a MenuClass and handles transitions between pages.
public class ParentClass extends Sprite
{
public function ParentClass()
{
setupOptionMenu();
}
private function setupOptionMenu() : void
{
var myMenu:MenuClass = new MenuClass;
myMenu.setUpButtons();
this.addChild( myMenu );
}
+ public function transitionForward() : void
+ public function transitionBackward() : void
}
And here's the MenuClass which creates forward and backward buttons. Clicking each will tell the above ParentClass to transitionForward() or transitionBackward() respectively.
public class MenuClass extends Sprite
{
public function MenuClass()
{
setupButtons();
}
public function setUpButtons() : void
{
var backButton:Button = new Button();
backButton.addEventListener( MouseEvent.CLICK, backClick );
addChild( backButton );
var forwardButton:Button = new Button();
forwardButton.addEventListener( MouseEvent.CLICK, forwardClick );
addChild( forwardButton );
}
private function backClick( e:Event ) : void
{
ParentClass( this.parent ).transitionForward();
}
private function forwardClick( e:Event ) : void
{
ParentClass( this.parent ).transitionBackward();
}
}
One one hand, I feel like the MenuClass is too tightly bound to its parent and is thus less reusable. On the other hand I feel like, in the interest of keeping everything self contained, it's not the ParentClass's responsibility to reach into the MenuClass instance and add event listeners there. This seems like there should be some kind of OOP rule-of-thumb to govern this kind of relationship.
So my question is: Is my MenuClass too tightly bound to its parent? And if so, do you have any suggestions that would loosen that coupling?
Thanks.