Guest User

Untitled

a guest
Feb 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. /*
  2. Some design points:
  3. 1. IRobot interface is used because of there arn't basic functioanality and I'd like to preffer implementation before inheritance.
  4. 2. IResult<T> interface to describe returned type and incaplsulate logic in each of IChip type.
  5. */
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10.  
  11. namespace HomeTaskRobot
  12. {
  13. class Program
  14. {
  15. /// <summary>
  16. /// Execute chip operation
  17. /// </summary>
  18. public interface IChip
  19. {
  20. /// <summary>
  21. /// Chip oparation contracr
  22. /// </summary>
  23. /// <typeparam name="T">Custom returned type</typeparam>
  24. /// <returns></returns>
  25. IResult<T> Execute<T>();
  26. }
  27.  
  28. /// <summary>
  29. /// Generic chip's result
  30. /// </summary>
  31. /// <typeparam name="T"></typeparam>
  32. public interface IResult<T>
  33. {
  34. /// <summary>
  35. /// Generic result
  36. /// </summary>
  37. T Result { get; set; }
  38. }
  39.  
  40. /// <summary>
  41. /// Robot interface
  42. /// </summary>
  43. public interface IRobot
  44. {
  45. /// <summary>
  46. /// Getting unique chips count
  47. /// </summary>
  48. int InstalledChipUniqueTypesCount { get; }
  49.  
  50. /// <summary>
  51. /// Set chip for some operation type
  52. /// </summary>
  53. /// <param name="chip"></param>
  54. void SetChip(IChip chip);
  55.  
  56. /// <summary>
  57. /// Executing installed chip operation
  58. /// </summary>
  59. /// <typeparam name="T">Returned type</typeparam>
  60. /// <returns></returns>
  61. T Execute<T>();
  62. }
  63.  
  64. public class Robot : IRobot
  65. {
  66. private IChip _currentChip;
  67. private readonly object _lockObject = new object();
  68. private readonly HashSet<string> _uniqueChipTypes = new HashSet<string>();
  69.  
  70. /// <summary>
  71. /// Get the unique installed chips count
  72. /// </summary>
  73. public int InstalledChipUniqueTypesCount => _uniqueChipTypes.Count;
  74.  
  75. /// <summary>
  76. /// Set current chip
  77. /// </summary>
  78. /// <param name="chip"></param>
  79. public void SetChip(IChip chip)
  80. {
  81. if (chip == null)
  82. throw new ArgumentNullException("chip to set");
  83.  
  84. lock (_lockObject)
  85. {
  86. _currentChip = chip;
  87. _uniqueChipTypes.Add(chip.GetType().FullName);
  88. }
  89.  
  90. }
  91.  
  92. /// <summary>
  93. /// Executing installed chip operation
  94. /// </summary>
  95. /// <typeparam name="T">Returned type</typeparam>
  96. /// <returns></returns>
  97. public T Execute<T>()
  98. {
  99. if (_currentChip == null)
  100. throw new ArgumentNullException("chip to set");
  101.  
  102. return _currentChip.Execute<T>().Result;
  103. }
  104. }
  105.  
  106. /// <summary>
  107. /// Chip with sorting implementation
  108. /// </summary>
  109. public class SortableChip : IChip
  110. {
  111. private List<int> _list;
  112. private bool _isAsc = true;
  113.  
  114. public SortableChip(List<int> list)
  115. {
  116. if (list == null)
  117. throw new ArgumentNullException();
  118.  
  119. _list = list;
  120. }
  121.  
  122. /// <summary>
  123. /// Execute sortable chip operation
  124. /// </summary>
  125. /// <typeparam name="T">Custom returned type</typeparam>
  126. /// <returns></returns>
  127. public IResult<T> Execute<T>()
  128. {
  129. if (_isAsc)
  130. return (IResult<T>)new SortableResult { Result = _list.OrderBy(x => x).ToList() };
  131. else
  132. return (IResult<T>)new SortableResult { Result = _list.OrderByDescending(x => x).ToList() };
  133. }
  134.  
  135. /// <summary>
  136. /// Setting sorting tupe operation
  137. /// </summary>
  138. public void SetAsc()
  139. {
  140. _isAsc = true;
  141. }
  142.  
  143. /// <summary>
  144. /// Setting sorting tupe operation
  145. /// </summary>
  146. public void SetDesc()
  147. {
  148. _isAsc = false;
  149. }
  150.  
  151. /// <summary>
  152. /// Generic result implementation
  153. /// </summary>
  154. public class SortableResult : IResult<List<int>>
  155. {
  156. public List<int> Result { get; set; }
  157. }
  158. }
  159.  
  160. /// <summary>
  161. /// Chip with sorting implementation
  162. /// </summary>
  163. public class SummarableChip : IChip
  164. {
  165. private readonly List<int> _list;
  166.  
  167. public SummarableChip(List<int> list)
  168. {
  169. if (list == null)
  170. throw new ArgumentNullException();
  171.  
  172. _list = list;
  173. }
  174.  
  175. /// <summary>
  176. /// Execute sortable chip operation
  177. /// </summary>
  178. /// <typeparam name="T">Custom returned type</typeparam>
  179. /// <returns></returns>
  180. public IResult<T> Execute<T>()
  181. {
  182. var res = new SumResult() { Result = _list.Sum() };
  183. return (IResult<T>)res;
  184. }
  185.  
  186. /// <summary>
  187. /// Generic result implementation
  188. /// </summary>
  189. public class SumResult : IResult<int>
  190. {
  191. public int Result { get; set; }
  192. }
  193. }
  194.  
  195. static void Main(string[] args)
  196. {
  197. var originalList = new List<int> { 1, 7, 5, 2, 25 };
  198.  
  199. var robot = new Robot();
  200.  
  201. var sortableChip = new SortableChip(originalList);
  202. sortableChip.SetAsc();
  203.  
  204. robot.SetChip(sortableChip);
  205. var sortResultChipAscResult = robot.Execute<List<int>>();
  206.  
  207. sortResultChipAscResult.ForEach(i => Console.Write($" {i}"));
  208. Console.WriteLine();
  209.  
  210. sortableChip.SetDesc();
  211. robot.SetChip(sortableChip);
  212. var sortResultChipDescResult = robot.Execute<List<int>>();
  213.  
  214. sortResultChipDescResult.ForEach(i => Console.Write($" {i}"));
  215. Console.WriteLine();
  216.  
  217.  
  218. var summarableChip = new SummarableChip(originalList);
  219. robot.SetChip(summarableChip);
  220.  
  221. var summResult = robot.Execute<int>();
  222.  
  223. Console.WriteLine(summResult);
  224.  
  225. Console.WriteLine(robot.InstalledChipUniqueTypesCount);
  226. }
  227. }
  228. }
Add Comment
Please, Sign In to add comment