Flex/Flash/Actionscript

Why doesn’t Panel extend LayoutContainer?

As I’m doing some writing for the Flex for Dummies book I was digging into the source code for Panel and the other containers. I was writing about how Panel allows you to set the layout property to either absolute, vertical, or horizontal, just like LayoutContainer (or Application, which extends LayoutContainer). My initial thought was that Panel probably just extends LayoutContainer and that’s where we get the layout property from. But then digging in I saw that both LayoutContainer and Panel independently implement the layout property (in basically a cut and paste exact same way). Unless I’m missing something there’s no reason why Panel shouldn’t just extend LayoutContainer. (BTW, for a recent writeup of the LayoutContainer class see Ben Clinkinbeard’s post here).

If you look at the setter for the layout property it’s almost identical in both Panel and LayoutContainer. And take a look in measure() and updateDisplayList() and you’ll see the same calls to layoutObject.measure() and layoutObject.updateDisplayList().

So I dunno, just thought it was interesting. Really I’m just procrastinating while I’m supposed to be writing. 😛 OK, back to work…

Standard

2 thoughts on “Why doesn’t Panel extend LayoutContainer?

  1. Inheritance would be easy… but in the end your subclasses would become heavily dependent on the parent implementation. This shouldn’t be a problem as long as you understand the implications and you are fine with it. Deep object hierarchies can and usually do become a problem. Before you say yes to inheritance you should consider composition (which is what they are doing). The duplication buys you improved encapsulation/compartmentalization and can insulate your application from or ease the pains of refactoring when parent implementations change. Just a thought. Peace.

Comments are closed.