cpe200-160 / CalculatorLab

Computer Engineering Lab for 261200
13 stars 241 forks source link

Design patterns #1

Open ahmethudaiaydin opened 6 years ago

ahmethudaiaydin commented 6 years ago

Try not to use "switch-case". Do you think it is possible?

Wanarut commented 6 years ago

which where?

ahmethudaiaydin commented 6 years ago

Every switch. Specially, for the math operators ie: '+', '-' ....

Wanarut commented 6 years ago

I can use if else but the code will be long. so,I will use switch case for choose way to calculate.

ahmethudaiaydin commented 5 years ago

No, I'm not talking about using if else. You can create classes for every math operator then use these classes in your code without an if or switch-case. Like:

abstract class MathOperator{ abstract string sign{get;set} // "+", "-", "/" or "*" abstract int process(int firstValue, int secondValue); } class PlusOperation :MathOperator { string sign { get {return "+";}} int process(int first, int second){ return first + second; } }

class MinusOperation: MathOperator{ ..... }

You can collect a new instance of these operations and when you want to calculate, you call call process method in a foreach.