Layouts as composites

I was looking at
the new tutorial on creating a title bar
and noticed that it uses layouts directly as component composites. This approach works when you’re set on using a specific layout, but it is rather counter-intuitive IMHO.

Is there a downside (other than writing about three lines of extra code) to using a CustomComponent instead and setting the layout as the composition root of the component? That way the layout can be changed on-the-fly without affecting the public interface of the component.

(The other advantage is a lot more subjective, but to me it feels more intuitive to create a custom component (which deals with its layout internally) than to create a layout and add some components to it.)

I agree. I have always recommended to use CustomComponent as base of your own composites (in contrast to using layouts directly) mainly because of this better encapsulation. You get:

  • Clean component API (no inherited layout methods)
  • Possibility to swap the underlying layout

Only downside I can think of is the extra level that is added to component tree structure.

IMHO this is good OO design and worth the couple of extra lines of code… and as you said: in most cases it is also more intuitive :slight_smile: