140 likes | 256 Views
Family Polymorphism & Nested Inheritance. Yevgen Voronenko. Problem: Java 1.4-. class Node { bool touches( Edge e) { ...e...} } class Edge { Node n1, n2; } class OnOffNode extends Node bool touches( Edge e) { ...( OnOffEdge )e... } }
E N D
Family Polymorphism &Nested Inheritance Yevgen Voronenko
Problem: Java 1.4- class Node { bool touches(Edge e) { ...e...} } class Edge { Node n1, n2; } class OnOffNode extends Node bool touches(Edge e) { ...(OnOffEdge)e... } } class OnOffEdge extends Edge { bool enabled; } ... new Edge(new Node, new Node) OK new OnOffEdge(new OnOffNode, new OnOffNode) OK new OnOffEdge(new OnOffNode, new Node) Runtime Error
What We Have Node Edge OnOffNode OnOffEdge
What We Want Node Edge Families OnOffNode OnOffEdge
Solution 2: gbeta class Graph { class Node { bool touches(Edge e) { ...e...} } class Edge { Node n1, n2; } } class OnOffGraph extends Graph { class Node { bool touches(Edge e) { if(enabled)...e... } } class Edge { bool enabled; } } new Graph.Edge(new Graph.Node, new Graph.Node) Compile Error final g1=new Graph; new g1.Edge(new g1.Node, new g1.Node); OK final g2=new OnOffGraph; new g2.Edge(new g2.Node, new g2.Node); OK new g2.Edge(new g1.Node, new g2.Node); Compile Error
Solution 2: gbeta Compile Time Graph Node Edge NotFamilies OnOffGraph Node Edge Runtime g1 : Graph Family Node Edge
Solution 3: Jx class Graph { class Node { bool touches(Edge e) { ...e...} } class Edge { Node n1, n2; } } class OnOffGraph extends Graph { class Node { bool touches(Edge e) { if(enabled)...e... } } class Edge { bool enabled; } } new Graph.Edge(new Graph.Node, new Graph.Node) Compile Error final Graph g1 = new Graph; new g1.class.Edge(new g1.class.Node, new g1.classNode); OK final Graphg2 = new OnOffGraph; new g2.class.Edge(new g2.class.Node, new g2.class.Node); OK new g2.class.Edge(new g1.class.Node, new g2.class.Node); Compile Error new g1.class.Edge(new g1.class.Node, new g2.class.Node); ??
Solution 3: Jx Compile Time Graph Node Edge Families OnOffGraph Node Edge hard to understand new g1.class.Edge(new g1.class.Node, new g2.class.Node); ?? hard to write final Graph g = new OnOffGraph(); final g.class.Node n = new g.class.Node; final g.class.Edge e = new g.class.Edge(n, n) e.touches(n);
Jx “problem” A B B2 A2 B B2
Solution 4: Java 1.5+ class FNode[N extends FNode[N,E], E extends FEdge[N, E]] { bool touches(E e); } class FEdge[N extends FNode[N,E], E extends FEdge[N, E]] { N n1, n2; } class Node extends FNode[Node, Edge] { bool touches(Edge e) { ...e... }} class Edge extends FEdge[Node, Edge] { } class OnOffNode extends FNode[OnOffNode, OnOffEdge] { bool touches(OnOffEdge e) { ...e... }} class OnOffEdge extends FEdge[OnOffNode, OnOffEdge] { bool enabled; } new Edge(new Node, new Node) OK new OnOffEdge(new OnOffNode, new OnOffNode) OK new OnOffEdge(new Node, new OnOffNode) Compile Error
Construction gbeta final Graph g = new OnOffGraph new g.Edge(new g.Node, new g.Node) Jx final Graph g = new OnOffGraph new g.class.Edge(new g.class.Node, new g.class.Node) Java 1.5 new OnOffEdge(new OnOffNode, new OnOffNode)
Construction gbeta final Graph g = new OnOffGraph new g.Edge(new g.Node, new g.Node) Jx final Graph g = new OnOffGraph new g.class.Edge(new g.class.Node, new g.class.Node) Java 1.5 new OnOffEdge(new OnOffNode, new OnOffNode)
Invoking touches() gbeta final Graph g = new OnOffGraph final g.Node n = new g.Node final g.Edge e = new g.Edge(n, n) e.touches(n) Jx final Graph g = new OnOffGraph final g.class.Node n = new g.class.Node final g.class.Edge e = new g.class.Edge(n, n) e.touches(n) Java 1.5 OnOffNode n = new OnOffNode OnOffEdge e = new OnOffEdge(n, n) e.touches(n)
Side-by-side G = graph n=node e=edge