-
Notifications
You must be signed in to change notification settings - Fork 196
Observer Concepts
Eivind Gussiås Løkseth edited this page Sep 2, 2018
·
3 revisions
Algorithms do not serialize any interesting data. They publish a set of standard events that an observer can listen to gather the data (algorithms and observers in QuickGraph implement the Observer Pattern). For example, one can attach an observer that records the vertex predecessors to algorithms that compute the shortest path.
-
VertexPredecessorRecorderObserver
, creates a dictionary that links vertices to their parent edge, -
EdgePredecessorRecorderObserver
, create a dictionary that links vertices to their parent edge, -
VertexDistanceRecorderObserver
, stores the distance of vertices from the root vertex, -
VertexTimeStamperObserver
, stores the time instant where a vertex processing start and finishes
Observer instance are to be 'attached' to algorithms during the computation. All observer implement an Attach
method that returns a disposable instance. When disposing, this instance 'detached' the observer from the algorithm events.
IVertexListGraph<TVertex,TEdge> g = ...; // some graph instance
var dfs = new DepthFirstSearchAlgorithm<TVertex,TEdge>(g);
var predecessorRecorder = new VertexPredecessorRecorderObserver<TVertex,TEdge>();
using(predecessorRecorder.Attach(dfs)) // listen to dfs events, then unhook
dfs.Compute();