The Sum of the Parts
Composite Components

In the previouse tutorials we have focussed on the Context inner interface and the role it provides in the declaration by the component of the resources it needs as a part of the instantiation process. Generally speaking anything we declare within a Context interface can be viewed as a dependency releationship. In effect - if we declare something in a Context interface we are basically stating that our component can only exst if and only if all required context dependencies can be fully resolved.

Another type of relationship - composition - is the subject of this tutorial. Within a composition relationship a component can act as a container of other components. More specifically - an enclosing component may contain subsidiary components wherein the destruction of the enclosing component implies the destruction of all subsidiary components (i.e. strong aggregation). Support for composition is provided via the inner Parts interface combined with a <parts> element in our component definition. The following implementation class demonstrates the usage of a parts interface acting as a convinient type-safe accessor to internal resources.

Component Example


Demo.java Our demo component updated to reflect the usage of the inner Parts interface.

The deployment solution is presented below. The only difference to our earlier example is the usage of the <parts> element instead of a <context> element.

<component xmlns="dpml:metro" class="org.acme.Demo" name="demo">
    <component key="widget" type="org.acme.Widget">
        <entry key="color" method="BLUE"/>
Testing the component

The following testcase log output is for all practival purposes the same as the earlier context example. If important difference is that consumer application (in this case a testcase) is unaware of the implementation strategy (dependency fullfillment via context versus internal fullfilment via parts).

    [junit] Executing forked test.
    [junit] Running org.acme.test.DemoTestCase
    [junit] [18906] [INFO   ] (demo): located the color java.awt.Color[r=0,g=0,b=255]
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.906 sec