Guest User

Untitled

a guest
Nov 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. public class Path
  2. {
  3. public List<Segment> Segments { get; } = new List<Segment>();
  4. }
  5.  
  6. public class Segment
  7. {
  8. public int Property { get; }
  9.  
  10. public Path Path { get; }
  11.  
  12. ...
  13. }
  14.  
  15. public static class Test
  16. {
  17. public static Path CreatePath()
  18. {
  19. var path = new Path();
  20. var segment = new Segment(path, 42);
  21. return path;
  22. }
  23. }
  24.  
  25. public Segment(Path path, int property)
  26. {
  27. Property = property;
  28. Path = path;
  29.  
  30. path.Segments.Add(this); // <--
  31. }
  32.  
  33. namespace PathExample0
  34. {
  35. public class Path
  36. {
  37. public List<Segment> Segments { get; } = new List<Segment>();
  38. }
  39.  
  40. public class Segment
  41. {
  42. public int Property { get; }
  43.  
  44. public Path Path { get; }
  45.  
  46. public Segment(Path path, int property)
  47. {
  48. Property = property;
  49. Path = path;
  50.  
  51. path.Segments.Add(this);
  52. }
  53. }
  54.  
  55. public static class Test
  56. {
  57. public static Path CreatePath()
  58. {
  59. var path = new Path();
  60. var segment = new Segment(path, 42);
  61. return path;
  62. }
  63. }
  64. }
  65.  
  66. namespace PathExample1
  67. {
  68. public class Path
  69. {
  70. public List<Segment> Segments { get; } = new List<Segment>();
  71. }
  72.  
  73. public class Segment
  74. {
  75. public int Property { get; }
  76.  
  77. public Path Path { get; }
  78.  
  79. public Segment(Path path, int property)
  80. {
  81. Property = property;
  82. Path = path;
  83. }
  84. }
  85.  
  86. public static class Test
  87. {
  88. public static Path CreatePath()
  89. {
  90. var path = new Path();
  91. var segment = new Segment(path, 42);
  92. path.Segments.Add(segment);
  93. return path;
  94. }
  95. }
  96. }
  97.  
  98. namespace PathExample2
  99. {
  100. public class Path
  101. {
  102. private readonly List<Segment> segments = new List<Segment>();
  103.  
  104. public IReadOnlyList<Segment> Segments => segments;
  105.  
  106. public void AddSegment(int property)
  107. {
  108. var segment = new Segment(this, property);
  109. segments.Add(segment);
  110. }
  111. }
  112.  
  113. public class Segment
  114. {
  115. public int Property { get; }
  116.  
  117. public Path Path { get; }
  118.  
  119. public Segment(Path path, int property)
  120. {
  121. Property = property;
  122. Path = path;
  123. }
  124. }
  125.  
  126. public static class Test
  127. {
  128. public static Path CreatePath()
  129. {
  130. var path = new Path();
  131. path.AddSegment(42);
  132. return path;
  133. }
  134. }
  135. }
  136.  
  137. namespace PathExample3
  138. {
  139. public interface ISegment
  140. {
  141. Path Path { get; }
  142.  
  143. int Property { get; }
  144. }
  145.  
  146. public class Path
  147. {
  148. private readonly List<Segment> segments = new List<Segment>();
  149.  
  150. public IReadOnlyList<ISegment> Segments => segments;
  151.  
  152. public void AddSegment(int property)
  153. {
  154. var segment = new Segment(this, property);
  155. segments.Add(segment);
  156. }
  157.  
  158. private class Segment : ISegment
  159. {
  160. public int Property { get; }
  161.  
  162. public Path Path { get; }
  163.  
  164. public Segment(Path path, int property)
  165. {
  166. Property = property;
  167. Path = path;
  168. }
  169. }
  170. }
  171.  
  172. public static class Test
  173. {
  174. public static Path CreatePath()
  175. {
  176. var path = new Path();
  177. path.AddSegment(42);
  178. return path;
  179. }
  180. }
  181. }
  182.  
  183. public class Segment
  184. {
  185. private Segment(Path path, int property)
  186. {
  187. Path = path;
  188. Property = property;
  189. }
  190.  
  191. public int Property { get; }
  192. public Path Path { get; }
  193.  
  194. public static Segment AddToPath(Path path, int property)
  195. {
  196. var segment = new Segment(path, property);
  197. path.Segments.Add(segment);
  198. return segment;
  199. }
  200. }
  201.  
  202. public class Path
  203. {
  204. private readonly List<Segment> segments = new List<Segment>();
  205.  
  206. public IReadOnlyList<Segment> Segments => segments;
  207.  
  208. public Segment AddSegment(int property)
  209. {
  210. return Segment.AddToPath(this, property);
  211. }
  212. }
  213.  
  214. public class Path
  215. {
  216. private readonly List<Segment> segments = new List<Segment>();
  217.  
  218. public IReadOnlyList<Segment> Segments => segments;
  219.  
  220. public void AddSegment(int property)
  221. {
  222. AddSegment(new Segment(property));
  223. }
  224.  
  225. public void AddSegment(Segment segment)
  226. {
  227. if (segment.Path != null) throw ...;
  228. segment.Path = segment;
  229. segments.Add(segment);
  230. }
  231. }
  232.  
  233. public class Segment
  234. {
  235. public int Property { get; }
  236.  
  237. public Path Path { get; internal set; }
  238.  
  239. public Segment(int property)
  240. {
  241. Property = property;
  242. }
  243. }
Add Comment
Please, Sign In to add comment