Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //Full Tutorial on https://indiedevart.wordpress.com/
- //
- using System;
- namespace Command.Pattern
- {
- class Program
- {
- static void Main()
- {
- // Create receiver, command, and invoker
- IEnemy receiver = new Goblin();
- ICommand command = new Heal(receiver);
- Field invoker = new Field();
- // Set and execute command
- invoker.SetCommand(command);
- invoker.EnterField();
- invoker.ExitField();
- Console.ReadKey();
- }
- }
- interface ICommand
- {
- void Execute();
- void UnDo();
- }
- class Heal : ICommand
- {
- IEnemy _receiver;
- // Constructor
- public Heal(IEnemy receiver)
- {
- _receiver = receiver;
- }
- public void Execute()
- {
- Console.WriteLine(_receiver.Heal());
- }
- public void UnDo()
- {
- Console.WriteLine(_receiver.Damage());
- }
- }
- class Buff : ICommand
- {
- IEnemy _receiver;
- // Constructor
- public Buff(IEnemy receiver)
- {
- _receiver = receiver;
- }
- public void Execute()
- {
- Console.WriteLine(_receiver.BuffON());
- }
- public void UnDo()
- {
- Console.WriteLine(_receiver.BuffOFF());
- }
- }
- interface IEnemy
- {
- string Damage();
- string Heal();
- string BuffON();
- string BuffOFF();
- }
- class Goblin : IEnemy
- {
- public string BuffOFF()
- {
- return "Goblin enters the field-Buff is ON";
- }
- public string BuffON()
- {
- return "Goblin exits the field-Buff is OFF";
- }
- public string Damage()
- {
- return "Goblin exits the field- takes damage";
- }
- public string Heal()
- {
- return "Goblin enters the field- heals";
- }
- }
- class Dragon : IEnemy
- {
- public string BuffOFF()
- {
- return "Goblin enters the field-Buff is ON";
- }
- public string BuffON()
- {
- return "Goblin exits the field-Buff is OFF";
- }
- public string Damage()
- {
- return "Goblin exits the field- takes damage";
- }
- public string Heal()
- {
- return "Goblin enters the field- heals";
- }
- }
- class Field
- {
- private ICommand _command;
- public void SetCommand(ICommand command)
- {
- this._command = command;
- }
- public void EnterField()
- {
- _command.Execute();
- }
- public void ExitField()
- {
- _command.UnDo();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement