2

I am working with JavaFX, specifically with:

  • javafx.scene.shape.Rectangle
  • javafx.scene.shape.Ellipse
  • javafx.scene.shape.Line

I need additional methods so I created the interface MoreFunctions. I created three new classes that inherit from above classes and implement MoreFunctions, i.e.:

public class MyRectangle extends javafx.scene.shape.Rectangle implements MoreFunctions {
...
}

This is fine as long as MyRectangle and MyEllipse have different implementations of the additional methods. But where do I put methods that have the same implementation? I can't change the parent class since I can't modify the framework. Is a default interface method the only (feasible) way to go? But then what about common attributes that methods rely on?

EDIT: An example of a common method

public void toggleSelection() {
    if (!selected) {
        setStrokeWidth(5);
        setStroke(Color.RED);
        selected = true;
    }
    else {
        setStrokeWidth(0);
        selected = false;
    }
}

This requires

private boolean selected;
  • Probably not the answer your looking for, but having a utility class with static methods that take MoreFunctions as a parameter is probably what I would do. – JustinKSU Nov 12 '15 at 23:04
  • Can you give an example of a common method signature and a common attribute between the two classes ? – QuakeCore Nov 12 '15 at 23:11
  • If you need fields you can use the composite model and have each of your classes have an internal class that has member variables with common variables on it. – JustinKSU Nov 12 '15 at 23:16
  • @JustinKSU: But then I still have to declare the internal class in all of the MyX classes. Wouldn't I still have to duplicate the code for each class? – problemofficer - n.f. Monica Nov 12 '15 at 23:23
  • You can create that inner class in the constructor. Instead of "this.someMethod()" you would call "this.common.someMethod()". Not so bad. Common code would not be repeated in the three classes. You might some methods that forward to the common method, but you don't have a lot of options if you can't change your parents. – JustinKSU Nov 12 '15 at 23:42

1 Answers1

2

It sounds a bit like you are looking for mixin functionality, which doesn't really exist in Java. You might be able to simulate mixins using Java 8 functionality or via a special purpose 3rd party mixin support library.

However, things might be a bit more straight-forward for other developers if you use either pass through methods or composed objects. Each of your Shape subclasses can delegate the functionality to common classes implementing particular functionality (e.g. selection toggling), rather than relying on new language features like default methods.

You can see the difference in inheritable or inherent functionality versus a delegated or compositional approach by examining the way selection capability is handled in various JavaFX classes. ToggleButton has a selectedProperty, so it is directly implementing the selection functionality. However, ListView has a selectionModelProperty, so the selection modeling capability of the ListView is delegated to an associated class, rather than directly implemented in the ListView itself.

Of course, things get a little complicated when you have MVC style systems like JavaFX Controls, but you probably don't need to code your system to that level of complexity. Though, you might want to examine how CSS support is added to controls and consider implementing functions such as styling a selected shape using similar CSS based support, rather than coding the style directly in code.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406