如何制作具有多种类型节点的树并且每个节点可以在 java 中有多个子节点

How to make a tree having multiple type of nodes and each node can have multiple child nodes in java

基本上我正在尝试实现这样的东西,其中合作伙伴节点是 "type1",客户端节点是 "type2",用户节点是 "type3"。并且每个节点都可以有多个子节点。所以Partner1下可以有任意数量的客户端节点,同样客户端节点下可以有任意数量的用户。

我已经开始实现了,但是我卡住了now.The我写的代码如下。

public class ClientProperty {
    public class Root{}         //NodeType1

    public class Partner{       //NodeType2
        public String partner_id;
        public String partner_name;
        public int partner_node_id;

        public Partner(String partner_id,String partner_name,int partner_node_id){
            this.partner_id = partner_id;
            this.partner_name = partner_name;
            this.partner_node_id = partner_node_id;
        }
    }

    public class Clients{       //NodeType3
        public String client_name;
        public String client_id;
        public int client_node_id;
        public Map<Enum,List<Enum>> clientproperty = new HashMap<Enum,List<Enum>>();

        public Clients(String client_name, String client_id, int client_node_id,Map<Enum,List<Enum>> clientproperty){
            this.client_name = client_name;
            this.client_id = client_id;
            this.client_node_id = client_node_id;
            this.clientproperty = clientproperty;
        }
    }
    public class Users{         //NodeType4
        public String user_name;
        public String user_id;
        public int user_node_id;

        public Users(String user_id,String user_name, int user_node_id){
            this.user_id = user_id;
            this.user_name = user_name;
            this.user_node_id = user_node_id;
        }
    }
    public class Node{
        Node next;
        Object nodes;

        public Node(){
            next = null;
        }

        public Node(Object nodes, Node next){
            this.nodes = nodes;
            this.next = next;
        }
    }
}

如果需要一些见解,请告诉我

首先是一些不具体的事情:

您想阅读有关数据封装的内容。将所有 public 字段放在 类 上是完全错误的。你实际上想尽可能地隐藏这些信息。

然后您想阅读有关 java 编码风格约定;因为你违反了其中的一些(当你向更有经验的 java 编码人员展示你的代码时,这根本无济于事)。

最后,最重要的是:您想阅读很多关于 OO 设计 的一般内容(我推荐 Robert Martin 的 "Agile practices";有一个免费的 PDF那本书的 "C# version"):

首先是

a) 作为 client/user 比

更 "different responsibility"

b) 是图中的某个元素

换句话说:您在 类 中放入了太多 "roles"。

意思:你想引入各种抽象。例如:

interface GraphNode<N, C extends GraphNode> {
    N getNodeContent();
    List<C> getChildrenNodes();
}

现在你可以表达:任何 "node" 确实有一些内容(可以是用户或客户或任何 object);它有一个 children 的列表(或集合),它们也是 "nodes".

这里我给大家提供一个high-level的设计,大家要根据自己的要求给出getNext()和getChildren()的实现。 希望对您有所帮助,如果您有任何其他想法,请告诉我。

// As all the entities are a kind of Node , so this interface should be implemented by all the entities . You can put some more methods in this interface, If required to be a Node type.

interface Node {
    Node getNext();
    List<? extends Node> getChildren();
}

class Root implements Node {
    private List<Partner> partners;

    @Override /*Implementation required*/
    public Root getNext() {
        return null;   // Return next node
    }

    @Override /*Implementation required*/
    public List<Partner> getChildren() {
        return partners;
    }
}

class Partner implements Node  {
    private List<Client> clients;
    @Override /*Implementation required*/
    public Partner getNext() {
        return null;  // Return next node
    }

    @Override /*Implementation required*/
    public List<Client> getChildren() {
        return clients;
    }
}

class Client implements Node {
    private List<User> users;

    @Override /*Implementation required*/
    public Client getNext() {
        return null;   // Return next node
    }

    @Override /*Implementation required*/
    public List<User> getChildren() {
        return users;
    }
}

//As per your requirement, User class is leaf node, so you can return null in getChildren() call;
class User implements Node  {
    private List<? extends Node> children;

    @Override /*Implementation required*/
    public User getNext() {
        return null;  // Return next node
    }

    @Override /*Implementation required*/
    public List<? extends Node> getChildren() {
        return children;
    }
}

注意:这是一个高级设计。通过这种设计,如果将来需要,您可以在应用程序中引入更多类型。