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?

1 Answer 1


Just to avoid a possible misconception, in the MVC pattern, the Model part does not consist of a single class, but rather the Model encompasses all classes that are not interfacing with the user.
In simple terms:

  • View: The classes that show something to the user (they render output)
  • Controller: The classes that react to user actions
  • Model: Everything else.

Now to your question:

The algorithms and the list of their names belong in the Model part of your application.
The algorithms themselves are part of your business logic and belong in the model for that reason. And if the Model is maintaining a collection of algorithms, then it is only natural that the Model can also provide both Controller and View with a list of available algorithms.

Selection from the list can work in two ways:

  1. The Model just provides a list of names. The View communicates the selected name to the Controller, which asks the Model for an instance of an algorithm with the specified name.
    This is easy, but requires string compares in the Model and can wreak havoc with localization.
  2. The Model provides a set of (key, value) pairs, where the key is an opaque ID that the Model can use to retrieve the specific instance of an algorithm and the value is the default name of the algorithm.
    This takes a bit more work, but can be beneficial in localized applications (the View can show translated names without risk of breaking stuff) or in locally running applications (the Model could just use a pointer/reference to the actual algorithm object as key).
  • Let me see if I understand option 1. An example of how it works- in view: controller.setAlgorithm(<algorithm-name>) > in controller model.getAlgorithmByName(<algorithm-name>) > in model return Algorithm to controller > in controller model.setAlgorithm(algorithm). The controller gets the algorithm from the model with the name it recieved from the view, and sets this algorithm in the model (Strategy pattern). Or even better, controller simply calls model.setAlgorithmByName(<algorithm-name>), and the model figures out the algorithm and sets it for itself. Is this accurate?
    – Aviv Cohn
    May 8, 2014 at 16:33
  • @Prog: Yes, that is accurate. The choice between setAlgorithm and setAlgorithmByName mostly depends on if you want model to depend on the class that maintains the list of algorithms. May 8, 2014 at 16:36
  • What do you mean? You said that the model part of the app will be responsible for containing the list of algorithms, so the model class is going to depend on it anyway. If the controller uses model.getAlgorithmByName() and then setAlgorithm() the model has to maintain a list of algorithms and return the algorithm matching the name. And if the controller uses model.setAlgorithmByName(<name>), also. So model depends on the class that maintains the list of algorithms anyway. Am I missing something?
    – Aviv Cohn
    May 8, 2014 at 16:40
  • As I said at the beginning of my answer, the MVC Model contains everything that isn't interfacing with the user. This means that the Model will consist of multiple classes and these classes don't need to all be interconnected. It is perfectly fine to have several classes in your Model that don't know about each others existence. May 8, 2014 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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