1

I'm developing a calculator application for a university project. I've encapsulated each calculator operation such as add, multiply, square, etc in separate classes which all have a common superclass. Is this an example of the command or strategy pattern, they seem to be quite similar and I can't decide here.

Thanks.

Darren Findlay
  • 2,533
  • 2
  • 29
  • 46
  • 1
    [This question and its answers](http://stackoverflow.com/questions/3883692/strategy-pattern-vs-command-pattern) should help. – Ray Toal Mar 20 '14 at 15:41

2 Answers2

2

It's both. Many patterns overlap. Your calculator program uses the Strategy pattern to allow for multiple operations and possibly allows for new operations to be added later or dynamically added.

Each operation strategy is invoked in a way that conforms to the Command Pattern.

dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • 1
    Strategy for sure because of the operation varying by each subclass. However, unless each instance of a operation is unique, i.e., 1+3*4/5+2 would be 4 objects, it might not be command. – Fuhrmanator Mar 20 '14 at 22:07
  • There would be some kind of composite Command that be used to compute the entire equation which is made up of many sub operation commands. – dkatzel Mar 24 '14 at 04:03
1

Strategy Pattern is used at a point where you need to decide which algorithm to use e.g. How you would want to do add for different types of numbers

Command Pattern will be used when you are making calls e.g. Whether you want to call Add class which has different strategies of Add or something else

Javaboy
  • 2,044
  • 2
  • 20
  • 24