In any Wicket repeater component, what is a common pattern to use different component classes based on the model objects class? My current approach is something like this, but i guess there is a way better solution:
BaseClass
|- AClass
|- BClass
`- CClass
protected void populateItem(Item<BaseClass> item) {
BaseClass obj = item.getModelObject();
if (obj instanceof AClass) {
item.add(new APanel("content", Model.of((AClass) obj)));
} else if (obj instanceof BClass) {
item.add(new BPanel("content", Model.of((BClass) obj)));
} else if (obj instanceof CClass) {
item.add(new CPanel("content", Model.of((CClass) obj)));
}
}
You can use a factory pattern that is external to your main page. The code in the factory would look similar to what you already have.
protected void populateItem(Item<BaseClass> item) {
item.add(PanelFactory.getPanel("content", item.getModelObject());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With