using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp
{
class Program
{
public interface Command
{
void Execute();
void Undo();
}
public class NoCommand : Command
{
public void Execute() { }
public void Undo() { }
}
public class Light
{
public void On()
{
Console.WriteLine("I am ligthting!");
}
public void Off()
{
Console.WriteLine("I am off now!");
}
}
public class LightOnCommand : Command
{
protected Light light = null;
public LightOnCommand(Light _light)
{
light = _light;
}
public void Execute()
{
light.On();
}
public void Undo()
{
light.Off();
}
}
public class OneButtonRemoteControlWithoutUndo
{
private Command slot = null;
public OneButtonRemoteControlWithoutUndo() { }
public void setCommand(Command Command)
{
slot = Command;
}
public void ButtonPressed()
{
slot.Execute();
}
}
public class EightButtonsRemoteControlWithUndoButton
{
private List onCommand;
private List offCommand;
private Command undoCommand;
public EightButtonsRemoteControlWithUndoButton()
{
onCommand = new List (new Command[7]);
offCommand = new List (new Command[7]);
for (int i = 0; i < 7; ++i) {
onCommand[i] = new NoCommand();
offCommand[i] = new NoCommand();
}
undoCommand = new NoCommand();
}
public void setCommand(int slot, Command _onCommand, Command _offCommand)
{
onCommand[slot] = _onCommand;
offCommand[slot] = _offCommand;
}
public void onButtonWasPushed(int slot)
{
onCommand[slot].Execute();
undoCommand = onCommand[slot];
}
public void offButtonWasPushed(int slot)
{
offCommand[slot].Execute();
undoCommand = offCommand[slot];
}
public void undoButtonWasPressed()
{
undoCommand.Undo();
}
//TODO
//override ToString()
}
// Undo button in multistate situation
public class CeilingFan
{
public const int HIGH = 3;
public const int MEDIUM = 2;
public const int LOW = 1;
public const int OFF = 0;
private int speed;
private string location;
public CeilingFan(string _location)
{
location = _location;
speed = OFF;
}
public int Speed
{
get
{
return speed;
}
}
public void High()
{
speed = HIGH;
Console.WriteLine("I am high speed now!");
}
public void Medium()
{
speed = MEDIUM;
Console.WriteLine("I am medium speed now!");
}
public void Low()
{
speed = LOW;
Console.WriteLine("I am low speed now!");
}
public void Off()
{
speed = OFF;
Console.WriteLine("I am off now!");
}
}
public class CeilingFanHighCommand : Command
{
private CeilingFan ceilingFan;
private int preSpeed;
public CeilingFanHighCommand(CeilingFan _ceilingFan)
{
ceilingFan = _ceilingFan;
}
public void Execute()
{
preSpeed = ceilingFan.Speed;
ceilingFan.High();
}
public void Undo ()
{
switch (preSpeed) {
case 3:
break;
case 2:
ceilingFan.Medium();
break;
case 1:
ceilingFan.Low();
break;
case 0:
ceilingFan.Off();
break;
default:
throw new ArgumentOutOfRangeException
(" speed degree error!");
}
}
}
public static void Test()
{
Light light = new Light();
LightOnCommand lightOnCommand =
new LightOnCommand(light);
OneButtonRemoteControlWithoutUndo oneButtonRemote =
new OneButtonRemoteControlWithoutUndo();
oneButtonRemote.setCommand(lightOnCommand);
oneButtonRemote.ButtonPressed();
CeilingFan ceilingFan = new CeilingFan("Living room");
CeilingFanHighCommand ceilingFanHighCommand =
new CeilingFanHighCommand(ceilingFan);
EightButtonsRemoteControlWithUndoButton remote =
new EightButtonsRemoteControlWithUndoButton();
remote.setCommand(0, ceilingFanHighCommand, ceilingFanHighCommand);
remote.onButtonWasPushed(0);
remote.undoButtonWasPressed();
}
static void Main(string[] args)
{
Test();
}
}
}
设计模式-命令模式
最新推荐文章于 2014-11-11 16:19:19 发布