Java best practice: a more detailed class variable in a subclass

I am modeling a drawn flat chart in java for some algorithms. My main classes are:

public class Node {
    private String label;
}

and

public class Edge {
    private Node node0;
    private Node node1;
}

This works well for algorithms. To draw a graph, I extended the node class with positions:

public class GraphicalNode extends Node {
    private int x;
    private int y;
}

My problem is the class for drawn edges. I would like to write something like this:

public class GraphicalEdge extends Edge {
    private GraphicalNode node0;
    private GraphicalNode node1;
}

But I've never seen such a design before. And besides, the compiler needs a constructor public GraphicalNode(Node node0, Node node1)for the superclass.

Does anyone have an idea to realize this?

+4
source share
3 answers

Maybe I misunderstood something - they will be shown below - but ...

Covariance.

Edge, Node getNode(), extends Edge. :

class Node {}
class Edge {
    Node getNode();
}

class GraphicalNode extends Node {}
class GraphicalEdge extends Edge {
    // This really overrides the method, with a more specific return type!
    @Override
    GraphicalNode getNode();
}

, , , MCVE:

public class WhatIsCovariance
{
    public static void main(String[] args)
    {
        Node n0 = new Node();
        Node n1 = new Node();
        Edge e0 = new Edge(n0, n1);

        Node n = e0.getNode0(); // Works


        GraphicalNode gn0 = new GraphicalNode();
        GraphicalNode gn1 = new GraphicalNode();
        GraphicalEdge ge0 = new GraphicalEdge(gn0, gn1);

        GraphicalNode gn = ge0.getNode0(); // Works
    }
}

class Node
{
    private String label;
}

class Edge
{
    private Node node0;
    private Node node1;

    Edge(Node node0, Node node1)
    {
        this.node0 = node0;
        this.node1 = node1;
    }

    public Node getNode0()
    {
        return node0;
    }

    public Node getNode1()
    {
        return node1;
    }
}

class GraphicalNode extends Node
{
    private int x;
    private int y;
}

class GraphicalEdge extends Edge
{
    private GraphicalNode node0;
    private GraphicalNode node1;

    GraphicalEdge(GraphicalNode node0, GraphicalNode node1)
    {
        super(node0, node1);

        this.node0 = node0;
        this.node1 = node1;
    }

    @Override
    public GraphicalNode getNode0()
    {
        return node0;
    }

    @Override
    public GraphicalNode getNode1()
    {
        return node1;
    }

}

: Edge, Node ( , , a GraphicalEdge). GraphicalEdge, GraphicalNode.

​​ : Edge Node , :

void computeSomething(Edge edge) {
    Node n0 = edge.getNode();
    Node n1 = edge.getNode();
    ...
}

void run() {
    GraphicialEdge e = new GraphicalEdge(...);

    computeSomething(e);
}

, :

void drawSomething(GraphicalEdge edge) {
    GraphicalNode n0 = edge.getNode();
    GraphicalNode n1 = edge.getNode();
    ...
}

void run() {
    GraphicialEdge e = new GraphicalEdge(...);

    computeSomething(e); // Works
    drawSomething(e); // Works as well

    Edge edge = e;
    drawSomething(edge); // Does not work. A GraphicalEdge is required.
}

, , , , , ...

:

, , GraphicalEdge - GraphicalNode Node. , , :

interface Node {}
interface Edge { 
    Node getNode();
}

interface GraphicalNode extends Node {}
interface GraphicalEdge extends Edge { 
    @Override
    GraphicalNode getNode();
}

class DefaultEdge implements GraphicalEdge { ... }

generics, wero, , , , , Node. , - ColoredGraphicalNode, generic type. : , "" , , , .

+1

generics Edge Node

public class Edge<N extends Node> {
    private N node0;
    private N node1;
    public Edge(N n0, N n1) { this.node0 = n0; this.node1 = n1; }
}

GraphicalEdge

public class GraphicalEdge extends Edge<GraphicalNode> {
    public GraphicalEdge (GraphicalNode n0, GraphicalNode n1) { super(n0, n1); }
}

. . x y Node, .

+2

, - Node Point:

public interface Point {
    double getX();
    double getY();
}

:

public class Node implements Point {
    private String label;
    // ...
}

Edge, :

public class Edge {
    private Node node0;
    private Node node1;

    public List<Node> getNodes() {
    // ...
}

, :

public class PointsDrawer {

    public void drawPoints(List<? extends PointPoint> pointsToDraw) {
        // draw logic here
    }
}

:

Edge edge = new Edge();
// initialization here ...
PointsDrawer pd = new PointsDrawer();
pd.drawPoints(edge.getNodes());

, , x y Node. , Point, Map.

0

Source: https://habr.com/ru/post/1624943/


All Articles