Method Add() for different classes

C#, Interface, Operations for complex figures

by Ivan Gerasimenko

JavaScript

IntBox ibox1 = new IntBox();
            IntBox ibox2 = new IntBox();
            ibox1.Value = 3;
            ibox2.Value = 5;
            StringBox sbox1 = new StringBox();
            StringBox sbox2 = new StringBox();
            sbox1.Value = "3";
            sbox2.Value = "5";
            StringBox sbox = new StringBox();

            IntBox ibox = (IntBox)Executor.Test(ibox1, ibox2);
            sbox = (StringBox)Executor.Test(sbox1, sbox2);


public interface IOperation 
    {
        IOperation Add(IOperation operand);
    }

    public class IntBox : IOperation
    {
        public int Value { get; set; }
        public IOperation Add(IOperation oper)
        {
            IntBox ibox = new IntBox();
            ibox.Value = this.Value + ((IntBox)oper).Value;
            return ibox;
        }
    }
    public class StringBox : IOperation
    {
        public String Value { get; set; }
        public IOperation Add(IOperation oper)
        {
            StringBox sbox = new StringBox();
            sbox.Value = this.Value + ((StringBox)oper).Value;
            return sbox;
        }
    }

    public class Executor
    {
        public static IOperation Test(IOperation oper1, IOperation oper2)
        {
            IOperation oper = oper1.Add(oper2);
            return oper;
        }
    }