Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.Linq;
- using System.Threading.Tasks;
- class Program
- {
- static void Main(string[] args)
- {
- var pult = new Pult();
- var tv = new TV();
- var mcrwln = new Microwave();
- pult.SetCommand(new TVOnCommand(tv));
- pult.PressButton();
- pult.PressUndo();
- pult.SetCommand(new MicrowaveCommand(mcrwln, 3000));
- pult.PressButton();
- // pult.PressUndo();
- Console.ReadLine();
- }
- }
- interface ICommand
- {
- void Execute();
- void Undo();
- }
- class TV
- {
- public void On()
- {
- Console.WriteLine("Телевизор включен");
- }
- public void Off()
- {
- Console.WriteLine("Телевизор выключен");
- }
- }
- class TVOnCommand: ICommand
- {
- TV tv;
- public TVOnCommand(TV tvSet)
- {
- this.tv = tvSet;
- }
- public void Execute()
- {
- tv.On();
- }
- public void Undo()
- {
- tv.Off();
- }
- }
- class Pult
- {
- ICommand command;
- public Pult(){}
- public void SetCommand(ICommand com)
- {
- this.command = com;
- }
- public void PressButton()
- {
- this.command.Execute();
- }
- public void PressUndo()
- {
- this.command.Undo();
- }
- }
- class Microwave
- {
- public void StartCooking(int time)
- {
- Console.WriteLine("Подогреваем еду. Осталось: {0} секунд: ", time/1000);
- Thread.Sleep(time);
- Console.WriteLine("Еда готова");
- }
- public void StopCooking()
- {
- Console.WriteLine("Микроволновка выключена");
- }
- }
- class MicrowaveCommand: ICommand
- {
- Microwave microwave;
- int time;
- public MicrowaveCommand(Microwave m, int t)
- {
- this.microwave = m;
- this.time = t;
- }
- public void Execute()
- {
- this.microwave.StartCooking(this.time);
- }
- public void Undo()
- {
- this.microwave.StopCooking();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement