Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle composite pattern in a Wicket-ish way?

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)));
    }
}
like image 581
Imperative Avatar asked Jan 28 '26 01:01

Imperative


1 Answers

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());
}
like image 75
JeredM Avatar answered Jan 30 '26 15:01

JeredM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!