Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Some design points:
- 1. IRobot interface is used because of there arn't basic functioanality and I'd like to preffer implementation before inheritance.
- 2. IResult<T> interface to describe returned type and incaplsulate logic in each of IChip type.
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace HomeTaskRobot
- {
- class Program
- {
- /// <summary>
- /// Execute chip operation
- /// </summary>
- public interface IChip
- {
- /// <summary>
- /// Chip oparation contracr
- /// </summary>
- /// <typeparam name="T">Custom returned type</typeparam>
- /// <returns></returns>
- IResult<T> Execute<T>();
- }
- /// <summary>
- /// Generic chip's result
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public interface IResult<T>
- {
- /// <summary>
- /// Generic result
- /// </summary>
- T Result { get; set; }
- }
- /// <summary>
- /// Robot interface
- /// </summary>
- public interface IRobot
- {
- /// <summary>
- /// Getting unique chips count
- /// </summary>
- int InstalledChipUniqueTypesCount { get; }
- /// <summary>
- /// Set chip for some operation type
- /// </summary>
- /// <param name="chip"></param>
- void SetChip(IChip chip);
- /// <summary>
- /// Executing installed chip operation
- /// </summary>
- /// <typeparam name="T">Returned type</typeparam>
- /// <returns></returns>
- T Execute<T>();
- }
- public class Robot : IRobot
- {
- private IChip _currentChip;
- private readonly object _lockObject = new object();
- private readonly HashSet<string> _uniqueChipTypes = new HashSet<string>();
- /// <summary>
- /// Get the unique installed chips count
- /// </summary>
- public int InstalledChipUniqueTypesCount => _uniqueChipTypes.Count;
- /// <summary>
- /// Set current chip
- /// </summary>
- /// <param name="chip"></param>
- public void SetChip(IChip chip)
- {
- if (chip == null)
- throw new ArgumentNullException("chip to set");
- lock (_lockObject)
- {
- _currentChip = chip;
- _uniqueChipTypes.Add(chip.GetType().FullName);
- }
- }
- /// <summary>
- /// Executing installed chip operation
- /// </summary>
- /// <typeparam name="T">Returned type</typeparam>
- /// <returns></returns>
- public T Execute<T>()
- {
- if (_currentChip == null)
- throw new ArgumentNullException("chip to set");
- return _currentChip.Execute<T>().Result;
- }
- }
- /// <summary>
- /// Chip with sorting implementation
- /// </summary>
- public class SortableChip : IChip
- {
- private List<int> _list;
- private bool _isAsc = true;
- public SortableChip(List<int> list)
- {
- if (list == null)
- throw new ArgumentNullException();
- _list = list;
- }
- /// <summary>
- /// Execute sortable chip operation
- /// </summary>
- /// <typeparam name="T">Custom returned type</typeparam>
- /// <returns></returns>
- public IResult<T> Execute<T>()
- {
- if (_isAsc)
- return (IResult<T>)new SortableResult { Result = _list.OrderBy(x => x).ToList() };
- else
- return (IResult<T>)new SortableResult { Result = _list.OrderByDescending(x => x).ToList() };
- }
- /// <summary>
- /// Setting sorting tupe operation
- /// </summary>
- public void SetAsc()
- {
- _isAsc = true;
- }
- /// <summary>
- /// Setting sorting tupe operation
- /// </summary>
- public void SetDesc()
- {
- _isAsc = false;
- }
- /// <summary>
- /// Generic result implementation
- /// </summary>
- public class SortableResult : IResult<List<int>>
- {
- public List<int> Result { get; set; }
- }
- }
- /// <summary>
- /// Chip with sorting implementation
- /// </summary>
- public class SummarableChip : IChip
- {
- private readonly List<int> _list;
- public SummarableChip(List<int> list)
- {
- if (list == null)
- throw new ArgumentNullException();
- _list = list;
- }
- /// <summary>
- /// Execute sortable chip operation
- /// </summary>
- /// <typeparam name="T">Custom returned type</typeparam>
- /// <returns></returns>
- public IResult<T> Execute<T>()
- {
- var res = new SumResult() { Result = _list.Sum() };
- return (IResult<T>)res;
- }
- /// <summary>
- /// Generic result implementation
- /// </summary>
- public class SumResult : IResult<int>
- {
- public int Result { get; set; }
- }
- }
- static void Main(string[] args)
- {
- var originalList = new List<int> { 1, 7, 5, 2, 25 };
- var robot = new Robot();
- var sortableChip = new SortableChip(originalList);
- sortableChip.SetAsc();
- robot.SetChip(sortableChip);
- var sortResultChipAscResult = robot.Execute<List<int>>();
- sortResultChipAscResult.ForEach(i => Console.Write($" {i}"));
- Console.WriteLine();
- sortableChip.SetDesc();
- robot.SetChip(sortableChip);
- var sortResultChipDescResult = robot.Execute<List<int>>();
- sortResultChipDescResult.ForEach(i => Console.Write($" {i}"));
- Console.WriteLine();
- var summarableChip = new SummarableChip(originalList);
- robot.SetChip(summarableChip);
- var summResult = robot.Execute<int>();
- Console.WriteLine(summResult);
- Console.WriteLine(robot.InstalledChipUniqueTypesCount);
- }
- }
- }
Add Comment
Please, Sign In to add comment