Advertisement
yakovmonarh

Команда (Command) с добавлением микроволновки

Aug 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 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 pult = new Pult();
  13.     var tv = new TV();
  14.     var mcrwln = new Microwave();
  15.    
  16.     pult.SetCommand(new TVOnCommand(tv));
  17.    
  18.     pult.PressButton();
  19.     pult.PressUndo();
  20.    
  21.     pult.SetCommand(new MicrowaveCommand(mcrwln, 3000));
  22.     pult.PressButton();
  23. //      pult.PressUndo();
  24.    
  25.     Console.ReadLine();
  26.    }
  27. }
  28.  
  29. interface ICommand
  30. {
  31.     void Execute();
  32.     void Undo();
  33. }
  34.  
  35. class TV
  36. {
  37.     public void On()
  38.     {
  39.         Console.WriteLine("Телевизор включен");
  40.     }
  41.    
  42.     public void Off()
  43.     {
  44.         Console.WriteLine("Телевизор выключен");
  45.     }
  46. }
  47.  
  48. class TVOnCommand: ICommand
  49. {
  50.     TV tv;
  51.    
  52.     public TVOnCommand(TV tvSet)
  53.     {
  54.         this.tv = tvSet;
  55.     }
  56.    
  57.     public void Execute()
  58.     {
  59.         tv.On();
  60.     }
  61.    
  62.     public void Undo()
  63.     {
  64.         tv.Off();
  65.     }
  66. }
  67.  
  68. class Pult
  69. {
  70.     ICommand command;
  71.    
  72.     public Pult(){}
  73.    
  74.     public void SetCommand(ICommand com)
  75.     {
  76.         this.command = com;
  77.     }
  78.    
  79.     public void PressButton()
  80.     {
  81.         this.command.Execute();
  82.     }
  83.    
  84.     public void PressUndo()
  85.     {
  86.         this.command.Undo();
  87.     }
  88. }
  89.  
  90. class Microwave
  91. {
  92.     public void StartCooking(int time)
  93.     {
  94.         Console.WriteLine("Подогреваем еду. Осталось: {0} секунд: ", time/1000);
  95.         Thread.Sleep(time);
  96.         Console.WriteLine("Еда готова");
  97.     }
  98.    
  99.     public void StopCooking()
  100.     {
  101.         Console.WriteLine("Микроволновка выключена");
  102.     }
  103. }
  104.  
  105. class MicrowaveCommand: ICommand
  106. {
  107.     Microwave microwave;
  108.     int time;
  109.    
  110.     public MicrowaveCommand(Microwave m, int t)
  111.     {
  112.         this.microwave = m;
  113.         this.time = t;
  114.     }
  115.    
  116.     public void Execute()
  117.     {
  118.         this.microwave.StartCooking(this.time);
  119.     }
  120.    
  121.     public void Undo()
  122.     {
  123.         this.microwave.StopCooking();
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement