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.
UNLESS…
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: