2

I need to support UI client which has nested components. I have come up with below object model -

public class SomeUserInterface { 
        String name;
        List<Component> components;  
}
public class Component {
        List <Component> components;  
}

Here, SomeUserInterface will have multiple Components and each Component may have nested Components inside it. Is there any issue in the proposed object model? Or what is the best way to support nested components?

Note : SomeUserInterface and Component are not identical classes. SomeUserInterface can contain Component but vice-versa is not true.

5
  • What is the difference between a Container and a Component? Why have two apparently identical classes? Commented Oct 7, 2015 at 6:35
  • 2
    Don't define these as classes, make them interfaces. It leaves your inheritance model much more flexible for the future. Commented Oct 7, 2015 at 6:38
  • @AndyTurner - This is sample implementation. Yes, there will be Interfaces. Commented Oct 7, 2015 at 6:40
  • @Cinnam - can you please elaborate more? Commented Oct 7, 2015 at 6:48
  • This is called the Component Pattern Commented Oct 7, 2015 at 9:21

1 Answer 1

1

Both Component and Container can contain components. If they offer the same methods for adding/removing etc., they should inherit from a common ancestor:

public class ComponentHolder {
    List<Component> components;
}

public class Component extends ComponentHolder {
    // ...
}

public class Container extends ComponentHolder {
    String containerName;
    // ...
}

EDIT: Another possibility would be to use composition. That way your classes wouldn't need a common ancestor, but would still use the same component-list implementation. If you decided to change the way components are stored, you would do it in one place only.

public class ComponentHolder {
    List<Component> components;
    // add, remove, get, ...
}

public class Component {
    ComponentHolder content;
    void add(Component c) {
        content.add(c);
    }
    // ...
}

public class Container {
    String containerName;
    ComponentHolder content;
    // ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have edited my question (code as well as added a note). With your example, Container itself is a Component, which is not the case.
This seems useful but how about linking two logically unrelated classes like Container and Component via common parent i.e. ComponentHolder ?
@PST Even if they are otherwise unrelated, they share the ability to hold components and probably some methods like add, remove and others. If you don't want to inherit from a common base class, you could still have them implement a common interface, which would specify that an object has the component-holding functionality.
@PST if you don't want them to have a common ancestor at all but still want to share the functionality, you could use composition (see my edit).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.