Advertisement
yakovmonarh

Команда (Command)

Aug 22nd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 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.  
  7. class Program
  8. {
  9.    static void Main(string[] args)
  10.    {
  11.     var pult = new Pult();
  12.     var tv = new TV();
  13.    
  14.     pult.SetCommand(new TVOnCommand(tv));
  15.    
  16.     pult.PressButton();
  17.     pult.PressUndo();
  18.    
  19.     Console.ReadLine();
  20.    }
  21. }
  22.  
  23. interface ICommand
  24. {
  25.     void Execute();
  26.     void Undo();
  27. }
  28.  
  29. class TV
  30. {
  31.     public void On()
  32.     {
  33.         Console.WriteLine("Телевизор включен");
  34.     }
  35.    
  36.     public void Off()
  37.     {
  38.         Console.WriteLine("Телевизор выключен");
  39.     }
  40. }
  41.  
  42. class TVOnCommand: ICommand
  43. {
  44.     TV tv;
  45.    
  46.     public TVOnCommand(TV tvSet)
  47.     {
  48.         this.tv = tvSet;
  49.     }
  50.    
  51.     public void Execute()
  52.     {
  53.         tv.On();
  54.     }
  55.    
  56.     public void Undo()
  57.     {
  58.         tv.Off();
  59.     }
  60. }
  61.  
  62. class Pult
  63. {
  64.     ICommand command;
  65.    
  66.     public Pult(){}
  67.    
  68.     public void SetCommand(ICommand com)
  69.     {
  70.         this.command = com;
  71.     }
  72.    
  73.     public void PressButton()
  74.     {
  75.         this.command.Execute();
  76.     }
  77.    
  78.     public void PressUndo()
  79.     {
  80.         this.command.Undo();
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement