1

I have a class that could:

  1. Have multiple types of containers
  2. Have multiple types of implementations

and what I did to model so far is:

public interface ChildClass {
    Container getContainer();
    ...
}

and one of its impl (have a total of two, could be extended though):

public class ChildImplA implements ChildClass {
    ...
}

I have (as you might have already guessed) a Container interface:

public interface Container<T extends ChildClass> {
    List<T> getChildren();
}

and two interfaces extending Container:

public interface ContainerA<T extends ChildClass> extends Container<A> {
    List<T> getChildren();
}

and

public interface ContainerB<T extends ChildClass> extends Container<    
    List<T> getChildren();
}

The generic T refers to the implementations of ChildClass and a sample implementation of any of the last two would be like:

public class ContainerAImpl implements ContainerA<ChildImplA> {
}

I have a couple of related questions regarding how to (properly) model this in the database, and whether the design so far could have been better:

  1. I have a child_class table with container_a_id and container_b_id with foreign keys to container_a and container_b tables. Though one of the foreign keys has to be null at all times, is this still a better practice than having container_id that could reference multiple tables?
  2. Should I introduce a generic, say P to ChildClass as follows:
public interface ChildClass<C extends Container> {
    C getContainer();
    ...
}

and have 4 implementations of it in total (instead of 2 for the moment) as follows:

public class ContainerAChildImplA implements ChildClass<ContainerA> {
    ...
}

Please bear in mind that implementing 2nd question also brings in further complications (e.g. implementing a few other DAOs for different types of ChildClass implementations, instead of just doing:

getJdbcTemplate().update(child.getContainer() instanceof CintainerA ?
                        "INSERT INTO child_class VALUES container_a_id = ?" :
                        "INSERT INTO child_class VALUES container_b_id = ?" ,
                child.getContainer().getId());
Hasan Can Saral
  • 172
  • 1
  • 10
  • _"Have multiple types of parents"_ That sounds inherently flawed. – πάντα ῥεῖ Nov 03 '19 at 18:56
  • @πάνταῥεῖ I'm working on a rule and process engine. A rule can be a part of a `Process` or a `RuleGroup` where process engine is not deployed. I'd appreciate if you could explain what might be wrong with this? Also, your comment suggests your answer to question 2 would be yes to introduce the generic, am I right? – Hasan Can Saral Nov 03 '19 at 19:01
  • 1
    When you say parent, do you mean a parent class in the sense of inheritance ? Or do you mean a parent object of an instance of your class (e.g. a node in a tree) ? – Christophe Nov 03 '19 at 19:16
  • Oh sorry, I screwed up, should have been clearer. Editing now. I mean, container, not in inheritence meaning. – Hasan Can Saral Nov 03 '19 at 19:17
  • The example names of interfaces and classes it very difficult to understand the question. Also why are you counting implementations? There could be 0 or N implementations of any interface and that should not affect the question or the answers, so I feel I'm missing something here. – Tulains Córdova Nov 06 '19 at 15:09
  • It does not affect the question, at least not directly. Maybe because the implementations could increase by the order of it could. – Hasan Can Saral Nov 06 '19 at 15:28
  • Also, if you could suggest names for the classes/interfaces it could be of help, thanks. – Hasan Can Saral Nov 06 '19 at 15:30

0 Answers0