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
The contract is the
Factoryimplementer should construct the child
Componentusing 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.
Actually the components are intended to display data from an Object (a POJO, JSON or XML). For primitives in the object a simple
Component is enough. For arrays we need the
AggregateComponent. The factory is used in
AggregateComponent to separate the creation logic of child components.
As I mentioned earlier the key of child components should be in particular form. In future we may write a
Component to display maps (similar to array). In that case the key might be of form
parentComponent.key + cKey where
cKey is a key in the map.