IArithmeticOperator

If we want to perform arithmetic operations over an object which code is unreachable you must abstract these operations. IArithmeticOperator defines basic arithmetic operations, the objects that implements this interface must perform these procedures using two object parameters.

The members of this interface are:
Member Description
Add Sums the parameters and provides the result value.
Subtract Subtracts the parameters and provides the result value.
Multiply Multiplies the parameters and provides the result value.
Divide Divides the parameters and provides the result value.
Mod Divides the parameters and provides the remainder value.

Next code defines an Integer wrapper and an arithmetic operator for it.

An Integer wrapper:
public struct Integer
{
    private int value;
    public int Value
    {
        get { return value; }
    }

    public Integer(int value)
    {
        this.value = value;
    }

    public override string ToString()
    {
        return value.ToString();
    }
}

An IArithmeticOperator for the Integer object.
public class IntegerOperator: IArithmeticOperator
{
    public object Add(object left, object right)
    {
        if ((left is Integer) && (right is Integer))
        {
            int value = ((Integer)left).Value + ((Integer)right).Value;
            return new Integer(value);
        }
        throw new InvalidCastException();
    }

    public object Subtract(object left, object right)
    {
        int value = ((Integer)left).Value - ((Integer)right).Value;
        return new Integer(value);
    }

    public object Multiply(object left, object right)
    {
        int value = ((Integer)left).Value * ((Integer)right).Value;
        return new Integer(value);
    }

    public object Divide(object left, object right)
    {
        int value = ((Integer)left).Value / ((Integer)right).Value;
        return new Integer(value);
    }

    public object Mod(object left, object right)
    {
        int value = ((Integer)left).Value % ((Integer)right).Value;
        return new Integer(value);
    }
}

Testing the defined objects:
static void Main(string[] args)
{
    Integer leftValue = new Integer(16);
    Integer rightValue = new Integer(4);
    IArithmeticOperator op = new IntegerOperator();
    Console.WriteLine("leftValue: \t{0}", leftValue);
    Console.WriteLine("rightValue: \t{0}", rightValue);
    Console.WriteLine("===========================");
    Console.WriteLine("Sum: \t\t{0}", op.Add(leftValue, rightValue));
    Console.WriteLine("Subtract: \t{0}", op.Subtract(leftValue, rightValue));
    Console.WriteLine("Multiply: \t{0}", op.Multiply(leftValue, rightValue));
    Console.WriteLine("Divide: \t{0}", op.Divide(leftValue, rightValue));
    Console.WriteLine("Mod: \t\t{0}", op.Mod(leftValue, rightValue));
    Console.ReadLine();
}

Output:
Integer leftValue: 16
Integer rightValue: 4
===========================
Sum: 20
Subtract: 12
Multiply: 64
Divide: 4
Mod: 0

Last edited Jul 26, 2013 at 3:24 AM by rathmA, version 2

Comments

No comments yet.