Passing "set" action to child objects

Hi all, I'm developing a replacement for ControlP5 because I want something more sophisticated, but I've run into an issue.

All classes inherit most of their behavior except the way they are drawn from the GUI class. A GUI object has an ArrayList with other GUI objects in it called "children", and has the authority to do whatever it wants with them. Those children can have children of their own, and so on.

Now, the issue I have that I have a "sea of setters", and I'm trying to implement an "inheritance" functionality: I want to be able to call "inherit" for a GUI object, and then have all setters called thereafter also apply to all the GUI object's children. I currently handle it by having all GUI objects have a variable "inherit", and when this is set and a setter is called, the setter will also call all the children's setters. For instance:

 public class GUI {
      ...
      public GUI setPaddings(float padding) {
        this.paddingBottom = padding;
        this.paddingLeft = padding;
        this.paddingTop = padding;
        this.paddingRight = padding;
        if (inherit) {
          for (int i = 0; i < children.size(); i++) {
            children.get(i).setPaddings(padding);
          }
        }
        return this;
      }
    ...
    }

But my problem is that this approach doesn't scale well, as I'll have to change all these bits of code by hand if something about the inheritance system changes. Is there a design pattern for this? Tricks you could recommend? I looked at the way ControlP5 does this, but I strongly suspect them of using reflection which I don't want to resort to.

Answers

  • I'm developing a replacement for ControlP5 because I want something more sophisticated

    In what way more sophisticated?

    I find your description confusing because you are talking about inheritance but describing aggregation (i.e. a GUI object having an arraylist of GUI objects).

    Can you give some concrete example relating the GUI class, its children to an actual UI control?

Sign In or Register to comment.