I have the implemented a calculator that is very similar to the following UML diagram. The only new thing is a new class, a Recognizer class, that says if the expression is written correctly or not.


I have applied a factory design pattern for the operations Add,Sub,Extract,Division but I'm also interested in applying a structural pattern aswell(Adapter,Bridge,Filter,Composite,Decorator,Facade,Flyweight or Proxy). What would fit the diagram?

  • 2
    You're looking at it backward. Design patterns are not building blocks, you can't design an architecture by trying to fit as many design patterns as you can. – Vincent Savard Nov 13 '18 at 18:28
  • @VincentSavard I understand this, however it's for a project where I'm asked to add a structural pattern, alongside a creational and behaviour pattern. So I'm pretty much trying to get the best thing that can fit inside this, even if it looks horrible. – Shury Nov 13 '18 at 18:35

If you are obliged to chose a structural pattern, I'd suggest to revisit the Expression and its derivates with the Composite pattern:

  • every expression can be composed themselves of sub-expressions
  • some expressions can be binary (e.g. those that you already identified)
  • some expressions can be leafs (e.g. a variable or a constant literal used in an expression)

For this to work, you need to refactor evaluate to take no argument (or a map of variables with their values as argument).

Edit: this article has a nice (although simplified) example of a composite used for expressions. This question about expressions data structures has some answer using the composite pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.