Click here to Skip to main content
15,885,066 members
Articles / Desktop Programming / Windows Forms

Aspect Oriented Programming for Benchmarking

Rate me:
Please Sign up or sign in to vote.
4.64/5 (16 votes)
1 Jul 2007CPOL9 min read 74K   352   57  
This article explains how you can use AOP for benchmarking purposes.
//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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions