Please consider a program, where the user chooses an algorithm from a list, and the Strategy pattern is utilized to set this algorithm as the model's operation.
For example, an image procession application. There are a number of algorithms that can be used to manipulate an image (darken, brighten, contrast, etc). They are all encapsulated in objects, and the controller sets the model to use an algorithm from this list. The algorithms also need to be presented by name on the UI.
This is how it will work:
The user can select an algorithm from a list on the GUI. When he/she does, the controller is notified, and sets the suitable algorithm in the model. Later on, when the model is called model.operate()
, it will delegate the operation to it's current algorithm. Classic Strategy pattern.
Also, the view needs access to the names of the algorithms, in order to allow the user to choose one from a list.
My question is this:
Should I store the algorithms somewhere, where the controller will fetch them from when it needs to set an algorithm in the model? Or should the controller just instantiate a new algorithm whenever it needs to set an algorithm in the model?
If the answer is that it's good to store the algorithms somewhere - than where?
In the model? - makes sense because the model encapsulates the business logic and data of the app. But how will the view fetch the names of the algorithms?
In the controller? - makes sense because the controller is the one that sets the algorithms in the model. And also it can offer the view a method getAlgorithmNames()
. But technically it shouldn't hold business logic objects, which is what the algorithms are.
In a different class, where anyone interested can fetch the algorithms or their names from?
I'm sure there have been a lot of applications where the user selects an operation, and the appropriate algorithm is set in the model. How do other applications do this?