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?
MVC: Where should I store interchangeable algorithms used by the Model (whose names also need to be accessible to the View)?
design;design patterns;object oriented;mvc;strategy
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 actionsModel: 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: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.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).