user2504380
user2504380

Reputation: 545

Cyclomatic Generics Code Design

Trying to speak abstract: I try to design a generic pattern for a network of Components and directed Connectors. All Componenents are connected by Connectors. So, with no loss of generality, imagining a chain (very simple net) of Components and Connectors we have something like this: CompA -> ConnectorAB -> CompB -> ConnectorBC -> CompC ...et cetera

As the the Connectors should be actual objects holding with some properties and functions i cannot just implement something like a double chained list of Componenents.

The Components hold theier input- and output-connectors in a list and the connector hold their source- and target-component. I give you one example-run of mine:

interface Component<In extends Connector, Out extends Connector> { List<In> getIns();...}

interface Connector<S extends Component, T extends Component> { S getSource();...}

interface CompA<In extends Connector<?, CompA>, Out extends Connector<CompA, ?>> extends Component<In, Out> {}

interface CompB<In extends Connector<?, CompB>, Out extends Connector<CompB, ?>> extends Component<In, Out> {}

interface A_A extends Connector<CompA, CompA> {}

interface A_B extends Connector<CompA, CompB> {}

Problems:

  • i.) Cyclomatic generic depedencies: i could nest the generics infinitely (Component<Connector<Component.......>>>), instead i use raw types, which is fine, but it seems to be messy anyway.
  • ii.) What i want to express with CompA<In extends Connector<?, CompA>, Out extends Connector<CompA, ?>> is, that my CompA knows that it is connected to a Connector aiming at a CompA. Obviously thats not what i am doing syntactically.
  • iii.) A_A aa; aa.getSource(); //gives me a CompA, thats great. aa.getSource().getIns(); //gives me a List of raw Connectors, but i would like to have a list of partially defined connectors, they know they are referencing a compA aa.getSource().getIns().get(0).getTarget() // should give me a compA Written my code as it is, it is obvious that this doesnt work as i want it, however i thought ´aa.getSource().getIns();´ could work as the the In-Type is at least a Connector right?
  • I am asking for suggestions to implement this pattern (so to say, correct my code!). It has to be in java, but i dont have to use only java-generics. If you can accomplish this abstract pattern with non-java-generic patterns it might be fine (a pattern based on other java-language-features, or OO-patterns).

    I guess i missed a lot of information, so please ask for it if something is unclear.

    Upvotes: 1

    Views: 62

    Answers (1)

    Ben Schulz
    Ben Schulz

    Reputation: 6181

    I think the following will model your requirements best. You may still hit several variance and/or erasure issues. If you do, I would advise you to drop generics entirely rather than resorting to raw types.

    interface Component<SELF extends Component<SELF>> { Set<Connector<?, SELF>> getIns();...}
    
    interface Connector<SOURCE extends Component<SOURCE>, DESTINATION extends Component<DESTINATION>> { SOURCE getSource();...}
    
    interface CompA extends Component<CompA> {}
    
    interface CompB extends Component<CompB> {}
    
    interface A_A extends Connector<CompA, CompA> {}
    
    interface A_B extends Connector<CompA, CompB> {}
    

    Upvotes: 3

    Related Questions