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 programmer = new Programmer();
- var tester = new Tester();
- var marketolog = new Marketolog();
- List<ICommand> commands = new List<ICommand>
- {
- new CodeCommand(programmer),
- new TestCommand(tester),
- new AdvetizeCommand(marketolog)
- };
- var manager = new Manager();
- manager.SetCommand(new MacroCommand(commands));
- manager.StartProject();
- manager.StopProject();
- Console.ReadLine();
- }
- }
- interface ICommand
- {
- void Execute();
- void Undo();
- }
- class MacroCommand : ICommand
- {
- List<ICommand> commands;
- public MacroCommand(List<ICommand> c)
- {
- this.commands = c;
- }
- public void Execute()
- {
- foreach(ICommand c in commands)
- {
- c.Execute();
- }
- }
- public void Undo()
- {
- foreach(ICommand c in commands)
- {
- c.Undo();
- }
- }
- }
- class Programmer
- {
- public void StartCoding()
- {
- Console.WriteLine("Программист начинает писать код");
- }
- public void StopCoding()
- {
- Console.WriteLine("Программист завершает писать код");
- }
- }
- class Tester
- {
- public void StartTest()
- {
- Console.WriteLine("Тестер начинает тестирование");
- }
- public void StopTest()
- {
- Console.WriteLine("Тестер завершает тестирование");
- }
- }
- class Marketolog
- {
- public void Advetize()
- {
- Console.WriteLine("Маркетолог начинает рекламировать продукт");
- }
- public void StopAdvetize()
- {
- Console.WriteLine("Маркетолог завершает рекламную компанию");
- }
- }
- class CodeCommand : ICommand
- {
- Programmer programmer;
- public CodeCommand(Programmer p)
- {
- this.programmer = p;
- }
- public void Execute()
- {
- programmer.StartCoding();
- }
- public void Undo()
- {
- programmer.StopCoding();
- }
- }
- class TestCommand : ICommand
- {
- Tester tester;
- public TestCommand(Tester t)
- {
- this.tester = t;
- }
- public void Execute()
- {
- tester.StartTest();
- }
- public void Undo(){
- tester.StopTest();
- }
- }
- class AdvetizeCommand : ICommand
- {
- Marketolog marketolog;
- public AdvetizeCommand(Marketolog m)
- {
- this.marketolog = m;
- }
- public void Execute()
- {
- marketolog.Advetize();
- }
- public void Undo()
- {
- marketolog.StopAdvetize();
- }
- }
- class Manager
- {
- ICommand command;
- public void SetCommand(ICommand c)
- {
- this.command = c;
- }
- public void StartProject()
- {
- if(command != null)
- {
- command.Execute();
- }
- }
- public void StopProject()
- {
- if(command != null)
- {
- command.Undo();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement