MVC: How to correctly create a view for an unknown model


Suppose I have a superclass, Model. Suppose also I have two subclasses, NumberModel and StringModel, one which stores a number and the other stores a string. To implement the MVC scheme, I make two views, NumberView and StringView that can edit and display the model data.

I then have a list of Models. Unfortunately, now I do not know which Models handle numbers and strings, so if I wanted to create views for all of them, I would break the object-oriented paradigm.


I make a method in Model called constructView() that returns an appropriate view and override it in the subclasses. But suddenly, I've made the model aware of the view, and this too doesn't feel good in the MVC scheme.

What would be the proper way to implement this from a design perspective?

Maybe the visitor pattern/double dispatch is the way to go? It gets a bit convoluted, and is perhaps overly complex if you only have two models/views. It's perhaps not the most elegant solution, but here goes:

/* Adding a new model involves adding a new method to this base class. Not very
 * elegant but that's the way the pattern works...  */
public class View {
    public void render(AModel model) { /* implement in subclass */ }
    public void render(BModel model) { /* implement in subclass */ }

/* Providers provides the views with models, and serves to decouple
 * models from views */
public interface ModelProvider {
    public void renderWith(View view);

public class AModelProvider implements ModelProvider {
    private final AModel _model;
    public void renderWith(View  view) { view.render(_model); }

public class BModelProvider implements ModelProvider {
    private final BModel _model;
    public void renderWith(View  view) { view.render(_model); }

public class AView extends View {
    public void render(AModel model) {

public class BView extends View {
    public void render(BModel model) {

public class Controller {
    public void renderModels(List<ModelProvider> providers, View view) {
        for (ModelProvider provider : providers) {
            // if provider is an AModelProvider and view is an AView,
            // render(AModel) will be called. If provider is a BModelProvider
            // or if view is BView, nothing will happen since the empty
            // View base class methods will be called...