mardi 15 juin 2021

SOLID Principles with calculator example

I'm new getting into SOLID principles, and I'd like to apply what I learned in very simple calculator example, I want to know how to enhance my design further more .

I have my Program class as the following

   class Program
    {
        static void Main(string[] args)
        {
            Ioperation sum = new Sum();
            Ioperation subtract = new Subtract();
            Calculator calculator = new Calculator (5, 2, sum);
            Console.WriteLine( calculator.Calculate());
        }
    }

And class Calculator as the following:

    class Calculator {
        private int num1 ;
        private int num2;
        private Ioperation ioperation;
        public Calculator(int num1,int num2,Ioperation ioperation) {
            this.num1 = num1;
            this.num2 = num2;
            this.ioperation =  ioperation;
        }
        public int Calculate() {
            return ioperation.calculate(num1,num2);
        }
    }
    interface Ioperation
    {
        public int calculate(int num1, int num2);
    }

I have an interface Ioperation , and I have 2 different classes which implement this interface to perform the mathematical operation in different ways. is this design good?, and how can I enhance it?

Aucun commentaire:

Enregistrer un commentaire