public Calculate ( |
||
request | ||
Результат | void |
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); }
static void Main(string[] args) { Chain chain1 = new AddNumbers(); Chain chain2 = new SubstructNumbers(); Chain chain3 = new DivideNumbers(); Chain chain4 = new MultiplyNumbers(); chain1.SetNextChain(chain2); chain2.SetNextChain(chain3); chain3.SetNextChain(chain4); Numbers numbers = new Numbers(6, 12, "-"); chain1.Calculate(numbers); }
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); }