Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. using System;
  2.  
  3. namespace WzorceProjektowe
  4. {
  5. // Nie zmieniaj poniższej implementacji
  6. public interface ICommand
  7. {
  8. void Execute();
  9. }
  10.  
  11. class FirstCommand : ICommand
  12. {
  13. private string message = "Jestę programistę";
  14. public FirstCommand(string message )
  15. {
  16. this.message = message;
  17. }
  18.  
  19.  
  20. public void Execute()
  21. {
  22. Console.WriteLine("Pierwsze polecenie wypisuje: " + message );
  23. }
  24. }
  25.  
  26. // Nie zmieniaj poniższej implementacji
  27. class SecondCommand : ICommand
  28. {
  29. public void Execute()
  30. {
  31. Console.WriteLine("Drugie polecenie nie wypisuje niczego konkretnego");
  32. }
  33. }
  34.  
  35. class ThirdCommand : ICommand
  36. {
  37. public int number;
  38. public ThirdCommand(int number)
  39. {
  40. this.number = (number)*4;
  41.  
  42. }
  43.  
  44. public void Execute()
  45. {
  46. Console.WriteLine("Trzecie polecenie wykonuje obliczenia,które dają " + number );
  47. }
  48. }
  49.  
  50. // Nie zmieniaj poniższej implementacji
  51. class Invoker
  52. {
  53. private ICommand _onStart;
  54. private ICommand _onMiddle;
  55. private ICommand _onFinish;
  56.  
  57. public void SetOnStart(ICommand command)
  58. {
  59. this._onStart = command;
  60. }
  61.  
  62. public void SetOnMiddle(ICommand command)
  63. {
  64. this._onMiddle = command;
  65. }
  66.  
  67. public void SetOnFinish(ICommand command)
  68. {
  69. this._onFinish = command;
  70. }
  71.  
  72. public void ExecuteCommands()
  73. {
  74. if (this._onStart is ICommand)
  75. {
  76. this._onStart.Execute();
  77. }
  78.  
  79. if (this._onMiddle is ICommand)
  80. {
  81. this._onMiddle.Execute();
  82. }
  83.  
  84. if (this._onFinish is ICommand)
  85. {
  86. this._onFinish.Execute();
  87. }
  88. }
  89. }
  90.  
  91. // Nie zmieniaj poniższej implementacji
  92. class Program
  93. {
  94. static void Main()
  95. {
  96. Invoker invoker = new Invoker();
  97. invoker.SetOnStart(new FirstCommand("Jestę programistę"));
  98. invoker.SetOnMiddle(new SecondCommand());
  99. invoker.SetOnFinish(new ThirdCommand(2));
  100.  
  101. invoker.ExecuteCommands();
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement