如何使树具有多种类型的节点,并且每个节点在Java中可以具有多个子节点

问题描述:

基本上,我正在尝试实现类似的方法,其中合作伙伴节点的类型为 type1,客户端节点的类型为 type2,而用户节点的类型为 type3。每个节点可以具有多个子节点。因此,Partner1下可以有任意数量的客户端节点,类似地,客户端节点下也可以拥有任意数量的用户。

Basically i am trying to implement something like this, where partner node is of say "type1", client node is of "type2" and user nodes is of "type3". And each of the nodes can have multiple number of child nodes. So Partner1 can have any number of client nodes under it, similarly client nodes can have any number of users under it.

我已经开始执行了,但是现在我被困住了。我编写的代码如下。

I have started the implementation but i am stuck now.The code which i have written is as follows.

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;
        }
    }
}

让我知道是否需要一些见识

Let me know if some insights is required

首先要做一些更具体的事情:

First some more unspecific things:

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

You want to read about data encapsulation. Putting all public fields on your classes is simply wrong. You actually want to hide such information as far as possible.

然后您想阅读有关 java编码样式约定的信息;

Then you want to read about java coding style conventions; as you are violating quite some of them (which simply doesn't help when you show your code to more experienced java coders).

最后,最重要的是:您想这么做,因为您违反了其中的一些规则(当您向经验丰富的Java程序员展示代码时,这根本无济于事)。通常阅读有关 OO设计的文章(我推荐Robert Martin的敏捷实践;该书的 C#版本有免费的PDF):

Finally, most important: you want to read quite a bit about OO design in general (I recommend "Agile practices" by Robert Martin; there is a free PDF of the "C# version" of that book):

始于

a)作为客户/用户的责任不同于

a) being a client/user is a "different responsibility" than

b)是图中的某个元素

b) being some element in a graph

换句话说:您在类中放置了太多角色。

In other words: you are putting way too many "roles" into your classes.

含义:您想引入各种抽象。例如:

Meaning: you want to introduce various kinds of abstractions. For example:

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

现在您可以表达:任何节点确实都有一些内容(可以成为用户或客户或任何对象);并且有一个(或一组)孩子列表,也就是节点。

Now you can express: any "node" does have some content (which could be a User or Client or whatever object); and it has a list (or set) of children, that are also "nodes".