Advertisement
yakovmonarh

Макрокоманды

Aug 31st, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7.  
  8. class Program
  9. {
  10.    static void Main(string[] args)
  11.    {
  12.     var programmer = new Programmer();
  13.     var tester = new Tester();
  14.     var marketolog = new Marketolog();
  15.     List<ICommand> commands = new List<ICommand>
  16.     {
  17.         new CodeCommand(programmer),
  18.         new TestCommand(tester),
  19.         new AdvetizeCommand(marketolog)
  20.     };
  21.    
  22.     var manager = new Manager();
  23.     manager.SetCommand(new MacroCommand(commands));
  24.     manager.StartProject();
  25.     manager.StopProject();
  26.    
  27.     Console.ReadLine();
  28.    }
  29. }
  30.  
  31. interface ICommand
  32. {
  33.     void Execute();
  34.     void Undo();
  35. }
  36.  
  37. class MacroCommand : ICommand
  38. {
  39.     List<ICommand> commands;
  40.    
  41.     public MacroCommand(List<ICommand> c)
  42.     {
  43.         this.commands = c;
  44.     }
  45.    
  46.     public void Execute()
  47.     {
  48.         foreach(ICommand c in commands)
  49.         {
  50.             c.Execute();
  51.         }
  52.     }
  53.    
  54.     public void Undo()
  55.     {
  56.         foreach(ICommand c in commands)
  57.         {
  58.             c.Undo();
  59.         }
  60.     }
  61. }
  62.  
  63. class Programmer
  64. {
  65.     public void StartCoding()
  66.     {
  67.         Console.WriteLine("Программист начинает писать код");
  68.     }
  69.    
  70.     public void StopCoding()
  71.     {
  72.         Console.WriteLine("Программист завершает писать код");
  73.     }
  74. }
  75.  
  76. class Tester
  77. {
  78.     public void StartTest()
  79.     {
  80.         Console.WriteLine("Тестер начинает тестирование");
  81.     }
  82.    
  83.     public void StopTest()
  84.     {
  85.         Console.WriteLine("Тестер завершает тестирование");
  86.     }
  87. }
  88.  
  89. class Marketolog
  90. {
  91.     public void Advetize()
  92.     {
  93.         Console.WriteLine("Маркетолог начинает рекламировать продукт");
  94.     }
  95.    
  96.     public void StopAdvetize()
  97.     {
  98.         Console.WriteLine("Маркетолог завершает рекламную компанию");
  99.     }
  100. }
  101.  
  102. class CodeCommand : ICommand
  103. {
  104.     Programmer programmer;
  105.     public CodeCommand(Programmer p)
  106.     {
  107.         this.programmer = p;
  108.     }
  109.    
  110.     public void Execute()
  111.     {
  112.         programmer.StartCoding();
  113.     }
  114.    
  115.     public void Undo()
  116.     {
  117.         programmer.StopCoding();
  118.     }
  119. }
  120.  
  121. class TestCommand : ICommand
  122. {
  123.     Tester tester;
  124.     public TestCommand(Tester t)
  125.     {
  126.         this.tester = t;
  127.     }
  128.    
  129.     public void Execute()
  130.     {
  131.         tester.StartTest();
  132.     }
  133.    
  134.     public void Undo(){
  135.         tester.StopTest();
  136.     }
  137. }
  138.  
  139. class AdvetizeCommand : ICommand
  140. {
  141.     Marketolog marketolog;
  142.     public AdvetizeCommand(Marketolog m)
  143.     {
  144.         this.marketolog = m;
  145.     }
  146.    
  147.     public void Execute()
  148.     {
  149.         marketolog.Advetize();
  150.     }
  151.    
  152.     public void Undo()
  153.     {
  154.         marketolog.StopAdvetize();
  155.     }
  156. }
  157.  
  158. class Manager
  159. {
  160.     ICommand command;
  161.     public void SetCommand(ICommand c)
  162.     {
  163.         this.command = c;
  164.     }
  165.    
  166.     public void StartProject()
  167.     {
  168.         if(command != null)
  169.         {
  170.             command.Execute();
  171.         }
  172.     }
  173.    
  174.     public void StopProject()
  175.     {
  176.         if(command != null)
  177.         {
  178.             command.Undo();
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement