Skip to content
Snippets Groups Projects
Commit 17975243 authored by Kevin Lin's avatar Kevin Lin
Browse files

image-processing: Explain the adapter pattern

parent 1fa286b3
No related branches found
No related tags found
No related merge requests found
Pipeline #396510 passed with stages
in 44 seconds
......@@ -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
public class ExampleGraph implements Graph<Node> {
public List<Node> neighbors(Node node) {
if (isSource(node)) {
return sourceNeighbors(node);
} else if (isSink(node)) {
return sinkNeighbors(node);
} else {
return pixelNeighbors(node);
}
}
private List<Node> sourceNeighbors(Node node) {
...
}
private List<Node> sinkNeighbors(Node node) {
...
}
private List<Node> pixelNeighbors(Node node) {
...
}
}
```
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.
```java
public class ExampleGraph implements Graph<Node> {
public List<Node> neighbors(Node node) {
return node.neighbors();
}
}
public class Source implements Node {
public List<Node> neighbors() {
...
}
}
public class Sink implements Node {
public List<Node> neighbors() {
...
}
}
public class Pixel implements Node {
public List<Node> neighbors() {
...
}
}
```
### Reference implementation
`AdjacencyListSeamFinder(DijkstraSolver::new)`
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment