fork download
  1. using System;
  2.  
  3. public interface IMessageOutput
  4. {
  5. void Out(string message);
  6. }
  7.  
  8. public class ConsoleMessageOutput : IMessageOutput
  9. {
  10. public void Out(string message)
  11. {
  12. Console.WriteLine(message);
  13. }
  14. }
  15.  
  16. public class ExtendedConsoleOutput : IMessageOutput
  17. {
  18. public void Out(string message)
  19. {
  20. Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " " + message);
  21. }
  22. }
  23.  
  24. public class Worker
  25. {
  26. private IMessageOutput _output;
  27. public Worker(IMessageOutput output)
  28. {
  29. _output = output;
  30. }
  31.  
  32. public void DoWork()
  33. {
  34. for(int i = 0; i < 10; i++)
  35. {
  36. _output.Out("Counter: " + i);
  37. }
  38. }
  39. }
  40.  
  41. public class Test
  42. {
  43. public static void Main()
  44. {
  45. Worker worker = new Worker(new ConsoleMessageOutput());
  46. worker.DoWork();
  47.  
  48. Worker worker2 = new Worker(new ExtendedConsoleOutput());
  49. worker2.DoWork();
  50. // your code goes here
  51. }
  52. }
Success #stdin #stdout 0.03s 16208KB
stdin
Standard input is empty
stdout
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Counter: 7
Counter: 8
Counter: 9
13:45:13 Counter: 0
13:45:13 Counter: 1
13:45:13 Counter: 2
13:45:13 Counter: 3
13:45:13 Counter: 4
13:45:13 Counter: 5
13:45:13 Counter: 6
13:45:13 Counter: 7
13:45:13 Counter: 8
13:45:13 Counter: 9