Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. public partial class MainWindow : Window
  2. {
  3. public MainWindow()
  4. {
  5. InitializeComponent();
  6. Drawer drawer = new Drawer();
  7. //MyTree.Items.Add(new Link(new CollectionInformation(new List<IElement>())));
  8. MyTree.Items.Add(
  9. new CollectionInformation(new List<IElement>
  10. {
  11. new Link(new CollectionInformation(new List<IElement> {new Link(new Circle(drawer))})),
  12. new Link(null)}
  13. ));
  14. ((IDrawable)MyTree.Items[0]).Draw();
  15. }
  16. }
  17.  
  18. public class Circle: ICircleElement
  19. {
  20. private readonly Drawer _drawer;
  21.  
  22. public Circle(Drawer drawer)
  23. {
  24. _drawer = drawer;
  25. }
  26.  
  27. public string ElementName
  28. {
  29. get { return "Circle"; }
  30. set { throw new NotImplementedException(); }
  31. }
  32.  
  33. public string ElementDescription
  34. {
  35. get { return "Circle desc"; }
  36. set { throw new NotImplementedException(); }
  37. }
  38.  
  39. public void DoUpdate(object arg)
  40. {
  41. // ...
  42. }
  43.  
  44. public double Radius { get; set; }
  45. public void Draw()
  46. {
  47. _drawer.Draw(this as dynamic);
  48. }
  49. }
  50.  
  51. public interface ICircleElement: IElement, ICircleInformation
  52. {
  53. }
  54.  
  55. // common interface. All node uses this.
  56. public interface IElement: IDrawable
  57. {
  58. string ElementName { get; set; }
  59. string ElementDescription { get; set; }
  60. void DoUpdate(object arg);
  61. }
  62.  
  63. public interface IDrawable
  64. {
  65. void Draw();
  66. }
  67.  
  68. public interface ICircleInformation
  69. {
  70. double Radius { get; set; }
  71. }
  72.  
  73. public class Drawer
  74. {
  75. public void Draw(Circle thingToDraw)
  76. {
  77. Console.WriteLine("I have just drawed a circle with radius: " + thingToDraw.Radius);
  78. }
  79.  
  80. public void Draw(IElement anythingElse)
  81. {
  82. Console.WriteLine("Can't infere type");
  83. }
  84. }
  85.  
  86. public class Link : ILink
  87. {
  88.  
  89. // IObs is constructor injected. Still I from SOLID
  90. //private readonly IObs _obs;
  91.  
  92. public Link(IElement referencedElement/* , IObs obs */)
  93. {
  94. ReferencedElement = referencedElement;
  95. //_obs = obs;
  96. Click = new RelayCommand(UpdateLink, _ => true);
  97. }
  98. public string ElementName { get; set; } = "Link";
  99. public string ElementDescription { get; set; } = "It is a link";
  100.  
  101. public IElement ReferencedElement { get; }
  102.  
  103. // needed for WPF ItemsSource. We should not add this.
  104. public List<IElement> Children
  105. {
  106. get { return new List<IElement> {ReferencedElement}; }
  107. }
  108.  
  109. public ICommand Click { get; private set; }
  110.  
  111. public void UpdateLink(object arg)
  112. {
  113. // now comes the real question. What we expect of an IElement. Should it have DoUpdate, which propagates further
  114. // or check the next step ourselfs
  115. // ILink referencedElement = ReferencedElement as ILink;
  116. // if (referencedElement != null)
  117. // {
  118. // referencedElement.UpdateLink(arg);
  119. // }
  120. // propagation through external observer
  121. if (ReferencedElement == null)
  122. return;
  123. //_obs.Notify(this, arg);
  124. DoUpdate(arg);
  125. }
  126.  
  127. public void DoUpdate(object arg)
  128. {
  129. Console.WriteLine("I did stuff");
  130. }
  131.  
  132. public void Draw()
  133. {
  134. if (ReferencedElement != null)
  135. {
  136. ReferencedElement.Draw();
  137. }
  138. }
  139. }
  140.  
  141. // I from SOLID. Link shows only the reachable property and method. User doesn't have to know about click, and other stuff.
  142. public interface ILink : IElement
  143. {
  144. IElement ReferencedElement { get; }
  145. void UpdateLink(object arg);
  146. }
  147.  
  148. public class CollectionInformation : ICollectionInformation
  149. {
  150. public CollectionInformation(List<IElement> children)
  151. {
  152. ListOfChildren = children;
  153. }
  154. public List<IElement> ListOfChildren { get; }
  155.  
  156. public string ElementName { get; set; } = "Collection info";
  157.  
  158. public string ElementDescription { get; set; } = "desc";
  159.  
  160. public void DoUpdate(object arg)
  161. {
  162. Console.WriteLine("I did stuff");
  163. }
  164.  
  165. public void Draw()
  166. {
  167. foreach (IElement element in ListOfChildren)
  168. {
  169. element.Draw();
  170. }
  171. }
  172. }
  173.  
  174. // extended functionality type 2
  175. public interface ICollectionInformation: IElement
  176. {
  177. List<IElement> ListOfChildren { get; }
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement