mardi 25 juin 2019

Avoiding breaking the SOLID principles while tryning to make a composite class methods visibile to another package class

I'm preparing a university project for my java exam, the project consists of creating a network of Routers and to send packets to each other until the packet arrive to his destination following the shortest path.

The project requires to adehere to SOLID principles, and I am actually in a stall situation, probably because it's my first time with java.

I've created a NODE class representing the generic network node which has the only responsability to hold a name associated with a IP and nothing else, then I create a ROUTING class, it's responsability is to hold all known network route in a HashMap( the integer represent the traffic load and it's random) including showing and adding entries to the map with methods.

The NODE class incorporates the ROUTING class as a protected internal class object.

Now these classes are stored in the ROUTER package, for creating a network I've created another package called NETWORKSTARTER and a class in it called STARTER, this class purpose is to create a network and connect(add nodes to the router table) other entities of the network to each other.

As I was coding the connection method I've stumbled upon the fact that I cannot call the internal methods of NODE.ROUTING even if it's a protected class within the NODE class.

Trying to solve this setback I've tought of creating three methods into the NODE class that "wraps" the methods of ROUTING class, basically calling them with other name, this way the STARTER methods can call the methods of ROUTING trough NODE methods for adding nodes to the HashMap.

The question is this: Is this design breaking the SOLID principles?In particullary does the NODE class design break the single responsability principle?

public class NODE{
private String nodeName;
private int nodeIP;
protected ROUTING nodeRoutingModule;

public NODE(String x,int y){
this.nodeName=x;
this.nodeIP=y;
this.nodeRoutingModule = new ROUTING();
}

// setter and getters for name and IP

public void addRoute(NODE node){
this.nodeRoutingModule.addToMap(node);
}

public void showTable(){
this.nodeRoutingMdoule.showMap();
}
}

public class ROUTING{
private HashMap<NODE,Integer> routeTable;

public ROUTING(){
this.routeTable = new HashMap<>();
}

public void addToMap(Node node){
int load = ThreadLocalRandom.current().nextInt(0,11);
routeTable.put(node,load);
}

public void showMap(){
routeTAble.forEach.((t,u) -> {
system.out.println(t.getIp+" "+t.getName);
});
}

}

Aucun commentaire:

Enregistrer un commentaire