@@ -73,6 +73,62 @@ The generic type `V` is used throughout the `graphs` package to represent the ve
`Node`
: An [**adapter**](https://en.wikipedia.org/wiki/Adapter_pattern) for the `Graph` interface that delegates responsibility for `neighbors` to each individual `Node` rather than the entire `Graph`.
Without the `Node` interface, we would need to centralize all of the logic in the `Graph` implementation.
```java
publicclassExampleGraphimplementsGraph<Node>{
publicList<Node>neighbors(Nodenode){
if(isSource(node)){
returnsourceNeighbors(node);
}elseif(isSink(node)){
returnsinkNeighbors(node);
}else{
returnpixelNeighbors(node);
}
}
privateList<Node>sourceNeighbors(Nodenode){
...
}
privateList<Node>sinkNeighbors(Nodenode){
...
}
privateList<Node>pixelNeighbors(Nodenode){
...
}
}
```
By introducing the `Node` interface, the logic for the graph is divided into separate implementations. This improves encapsulation by enabling each implementation to maintain its own fields and logic separate from other implementations.