- aop.zip
- antlr.runtime.dll
- Benchmarking.sln
- Benchmarking
- Client
- CommandUI
- Common.Logging.dll
- Core
- MyApplication.CommandClient
- MyApplication.CommandCore
- Sorting
- Spring.Aop.dll
- Spring.Core.dll
|
//Extracted from: http://www.dofactory.com/Patterns/PatternCommand.aspx
//Copyright 2001 - 2007 Data & Object Factory. All rights reserved. Data & Object Factory�,
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace MyApplication.CommandCore
{
// "Command"
abstract class Command
{
public abstract void Execute();
public abstract void UnExecute();
}
// "ConcreteCommand"
class CalculatorCommand : Command
{
char @operator;
Single operand;
Calculator calculator;
// Constructor
public CalculatorCommand(Calculator calculator,
char @operator, Single operand)
{
this.calculator = calculator;
this.@operator = @operator;
this.operand = operand;
}
public char Operator
{
set { @operator = value; }
}
public Single Operand
{
set { operand = value; }
}
public override void Execute()
{
calculator.Operation(@operator, operand);
}
public override void UnExecute()
{
calculator.Operation(Undo(@operator), operand);
}
// Private helper function
private char Undo(char @operator)
{
char undo;
switch (@operator)
{
case '+': undo = '-'; break;
case '-': undo = '+'; break;
case '*': undo = '/'; break;
case '/': undo = '*'; break;
default: undo = ' '; break;
}
return undo;
}
}
// "Receiver"
class Calculator
{
private Single curr = 0;
public void Operation(char @operator, Single operand)
{
switch (@operator)
{
case '+': curr += operand; break;
case '-': curr -= operand; break;
case '*': curr *= operand; break;
case '/': curr /= operand; break;
}
Console.WriteLine(
"Current value = {0,3} (following {1} {2})",
curr, @operator, operand);
}
public Single CurrentValue
{
get { return curr; }
set { curr = value; }
}
}
// "Invoker"
public class User : IUser
{
// Initializers
private Calculator calculator = null;
private ArrayList commands = null;
private int current = 0;
public void Initialize()
{
calculator = new Calculator();
commands = new ArrayList();
current = 0;
}
public void Redo(int levels)
{
Console.WriteLine("\n---- Redo {0} levels ", levels);
// Perform redo operations
for (int i = 0; i < levels; i++)
{
if (current < commands.Count - 1)
{
Command command = commands[current++] as Command;
command.Execute();
}
}
}
public void Undo(int levels)
{
Console.WriteLine("\n---- Undo {0} levels ", levels);
// Perform undo operations
for (int i = 0; i < levels; i++)
{
if (current > 0)
{
Command command = commands[--current] as Command;
command.UnExecute();
}
}
}
public void Compute(char @operator, Single operand)
{
// Create command operation and execute it
Command command = new CalculatorCommand(
calculator, @operator, operand);
command.Execute();
// Add command to undo list
commands.Add(command);
current++;
}
public Single CurrentValue
{
get { return calculator.CurrentValue; }
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.