code_junkie

Keeping an object graph consistent

Nov 14th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. interface IGraphNode
  2. {
  3. IGraphNode From { get; set; }
  4. IGraphNode To { get; set; }
  5. }
  6.  
  7. IGraphNode A = new GraphNode();
  8. IGraphNode B = new GraphNode();
  9. A.To = B;
  10.  
  11. IGraphNode From
  12. {
  13. get { return from; }
  14. set
  15. {
  16. from = value;
  17. if (value.To != this) {
  18. value.To = this;
  19. }
  20. }
  21. }
  22.  
  23. IGraphNode To
  24. {
  25. get { return to; }
  26. set
  27. {
  28. to = value;
  29. if (value.From != this) {
  30. value.From = this;
  31. }
  32. }
  33. }
  34.  
  35. A.To = B;
  36. B.From = A;
  37.  
  38. IGraphNode A = new GraphNode();
  39. IGraphNode B = new GraphNode();
  40. IGraphNode C = new GraphNode();
  41.  
  42. A.To = B;
  43. C.To = B;
  44.  
  45. // what should B.From return?
  46.  
  47. IGraphNode A = new GraphNode();
  48. IGraphNode B = new GraphNode();
  49.  
  50. A.To = B;
  51. A = null; // should the reference from B keep the object
  52. // that A used to reference alive?
  53.  
  54. void ConnectNodes(IGraphNode a, IGraphNode b)
  55. {
  56. a.to = b;
  57. b.from = a;
  58. }
  59.  
  60. void ConnectNodes(IGraphNode b)
  61. {
  62. this.to = b;
  63. b.from = this;
  64. }
Add Comment
Please, Sign In to add comment