ChainOfResponsibilityDesignPattern.AddNumbers.Calculate C# (CSharp) Method

Calculate() public method

public Calculate ( Numbers request ) : void
request Numbers
return void
        public void Calculate(Numbers request)
        {
            if(request.calculationWanted == CalculationType.Add)
            {
                Debug.Log("Adding: " + request.number1 + " + " + request.number2 + " = " + (request.number1 + request.number2).ToString());
            }
            else if(nextInChain != null)
                nextInChain.Calculate(request);
            else
                Debug.Log ("Handling of request failed: " + request.calculationWanted);
        }

Usage Example

        void OnEnable()
        {
            Debug.Log("------------------");
            Debug.Log("CHAIN OF RESPONSIBILITY DESIGN PATTERN");

            // create calculation objects that get chained to each other in a sec
            Chain calc1 = new AddNumbers();
            Chain calc2 = new SubstractNumbers();
            Chain calc3 = new DivideNumbers();
            Chain calc4 = new MultiplyNumbers();

            // now chain them to each other
            calc1.SetNextChain(calc2);
            calc2.SetNextChain(calc3);
            calc3.SetNextChain(calc4);

            // this is the request that will be passed to a chain object to let them figure out which calculation objects it the right for the request
            // the request is here the CalculationType enum we add. so we want this pair of numbers to be added
            Numbers myNumbers = new Numbers(3, 5, CalculationType.Add);

            calc1.Calculate(myNumbers);

            // another example:
            Numbers myOtherNumbers = new Numbers(6, 2, CalculationType.Multiply);

            calc1.Calculate(myOtherNumbers);

            // or pass it to some chain object inbetween which will not work in this case:
            Numbers myLastNumbers = new Numbers(12, 3, CalculationType.Substract);

            calc3.Calculate(myLastNumbers);
        }
All Usage Examples Of ChainOfResponsibilityDesignPattern.AddNumbers::Calculate