Note that there are some explanatory texts on larger screens.

plurals
  1. POAlternative for multiple inheritance in Java during GUI development
    primarykey
    data
    text
    <p>I ran at the following dilemma: there's no multiple inheritance in Java but I need it, how to avoid it?</p> <p>Below is the reason why I started thinking about it.</p> <p>I was in need of a text box that has several specific properties and behavior (on focus and on blur events are handled). I developed DecoratedTextBox without hesitation:</p> <pre><code>public class DecoratedTextBox extends TextBox implements FocusHandler, BlurHandler { public void onBlur(BlurEvent event) { //cool feature implementation } public void onFocus(FocusEvent event) { //another cool feature } //other cool features } </code></pre> <p>My GUI started looking good, but I didn't take into account PasswordTextBox. It also must have the same properties and behaviour that DecoratedTextBox has. But PasswordTextBox is inhereted from TextBox and it is in fact another class hierarchy branch. At once I remembered that it would be great if TextArea also has all those cool properties and behavior etc.</p> <p>So what is wrong with my design that it leads to thoughts about multiple-inheritance? What must be done to satisfy mentioned requirements?</p> <p><strong>Some clarification</strong></p> <p>As a result I had to inherit from PasswordTextBox, TextArea and so on to take advantage of their features (these classes are from GWT library). But I cannot understand how to weave composition here.</p> <p><strong>Update</strong></p> <p>Correct me If I understood what Anders Johansen said in a wrong way.</p> <p>The solution should look like this:</p> <pre><code>public class DecoratedTextBox extend AbstractEventHandler { private TextBox textBox; //wrap TextBox methods public String getText() { return textBox.getText(); } } public class DecoratedPasswordTextBox extend AbstractEventHandler { private PasswordTextBox passwordTextBox; //wrap TextBox methods //... } public class DecoratedTextArea extend AbstractEventHandler { private TextAre textArea; //wrap TextBox methods //... } public abstract class AbstractEventHandler implements FocusHandler, BlurHandler { public void onBlur(BlurEvent event) { //default cool feature implementation } public void onFocus(FocusEvent event) { //another default cool feature implementation } } </code></pre> <p><strong>updated</strong></p> <p>I tried variants suggested by Anders Johansen and Hilbrand Bouwkamp, but in each case I ran at a problem that I have a method (its signature cannot be changed) that adds widgets and one of the args is Widget itself. So if I'm not subclassing from something that is a subclass of Widget, I break a lot of classes.</p> <p>Still continue thinking on solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload