I am designing a UI framework in Java. Every UI component, represented by the class Component
, in the framework is identified by a non null unchangeable String
key. So I get the key in the constructor itself and provided no setter.
Now I have another class AggregateComponent
which represents a component containing array of components. In the AggregateComponent
class a Factory
is used to create the child components. For some reasons the key of the child components should be parentComponent.key + index
, where index
is the array index of the child component. So I pass parentComponent.key + index
to Factory.create()
.
The contract is the
Factory
implementer should construct the childComponent
using the passed key.
We may enforce this contract in AggregateComponent
by checking the key of the created component against the passed key and throw an exception. But I am asking
Is there any solution (even a completely different design solution) without such contracts.