- We have to create a new class extending the
Vertex<E,V>
This class can contain the two new methods String getText()
and setText(String s)
.
private class DecoratedVertex extends Vertex<Integer,String>{
String text;
public String getText() {
return text;
}
public void setText(String t) {
this.text = t;
}
}
- Then you need to create a "factory" for the vertices and edges. A factory is an object passed to the graph, to which the graph delegates the creation of its vertices and edges. Our factory must implement the methods for creating vertices:
createVertex()
and a method for casting a Position into a Vertex. The same functions should be written in order to replace edges.
private class DecoratedGraphFactory extends DefaultGraphFactory<Integer,String>{
public Vertex<Integer,String> createVertex(){
return new DecoratedVertex();
}
public Vertex<Integer,String> castVertex(Position<?> p){
return (Vertex<Integer,String>) p;
}
}
-
Once the factory has been created, we have to pass it as a parameter for the graph we are creating:
Graph<Integer,String> g = new DefaultGraph<Integer,String>(new DecoratedGraphFactory());
-
All vertices of the new created graph have the new property
for(Position v1 : g.vertices()){
DecoratedVertex v = (DecoratedVertex) v1;
System.out.println(v.getText);
}