Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Extending components Best Practice

Lino Costa
6 years ago Aug 20, 2015 5:50am
Marko Grönroos
6 years ago Aug 20, 2015 6:23am

Your solution is sort of reversing the roles of a component and extension, and I don't see anything good in that. It looks like a potential anti-pattern to me, altough it's difficult to exactly point out why. You might want to add more than one extension to the component, or rather extend the component by inheritance and add extensions to that.

If you want to encapsulate the "resetable" behaviour, why not encapsulate it in the component, by having "ResetableTextField" that extends TextField by inheritance, and adds the extension internally. Then, you could have method to add the ResetButtonClickListener in the component.

esetableTextField tf = new ResetableTextField("Caption");
tf.addResetButtonClickListener(click -> tf.setValue("")); // Or whatever

The ResetButtonClickListener could still be internally handled in the extension, but that would be a private implementation matter in the ResetableTextField. (Are there really more than the obvious way to "reset" something?)

Lino Costa
6 years ago Aug 20, 2015 6:29am
Marko Grönroos
6 years ago Aug 20, 2015 6:34am