2

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.

enter image description here

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

Siddharth Sinha
  • 578
  • 2
  • 15
  • 35

2 Answers2

1

First some more unspecific things:

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.

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).

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):

It starts wit the fact that

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

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".

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Here I am providing you a high-level design, you have to give the implementation of getNext() and getChildren() as per your requirement. I hope It may help you, Let me know If you have any other thought.

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

Note: Its a high level design. With this design, you can introduce more types in your application, if required in future.

pbajpai
  • 1,303
  • 1
  • 9
  • 24