I work in Java
I created a nice structure of decorators for graph edges. I have base edge with two vertices, then I have a weighted decorator that adds a weight to an edge and then I have a oriented decorator that adds orientation to an edge. Decorators are implemented using interfaces and delegate methods (not by inheritance).
Now I would like to add another decorator – a flow decorator, corresponding to a flow edge. Flow edges have both direction and weight (the capacity), plus the flow. How do I implement something like this in Java? I would need something like
public class FlowEdge implements IEdge, IWeightedEdge, IOrientedEdge, IFlowEdge
{
private IEdge, IWeightedEdge, IOrientedEdge decorated;
private int flow;
//constructors, delegate methods...
but that’s obviously not possible.
Maybe the decorator is not the best pattern to use. I would like to achieve the separation of concerns (so I could have any combination of normal, weighted, oriented edges), but also need functionality of possibly multiple decorators in one class. Is this somehow possible? Or am I over-engineering this?
I let go of this whole decorator idea and implemented the edges just like normal inheritace.
Edge –> WeightedEdge –> OrientedEdge –> FlowEdge
It is maybe not as elegant as using decorations and I cannot now have unweighted oriented edge, but it is straightforward and easy.