Reputation: 3359
In classic MVC pattern, the model communicate with view through notify events.
But it seems that defining some basic interfaces for views, and making the model communicate with view through interfaces, would also be feasible and could still decouple the model and concrete view.
However, I've never heard about any MV* pattern using interface between view and model. Is there any obstacles for communicating in such way?
Upvotes: 2
Views: 766
Reputation: 73446
First of all, MVC is an architectural pattern: It describes the main parts of a system, their responsibilities and how they interact. It does not impose any specific implementation.
The original MVC for example was designed in 1979 by OOP pioneers in a Smalltalk context. In this dynamically typed language, there are no interfaces: objects communicate with other objects sending them messages, without even being sure that the receiver can deal with the messages.
Views have the responsibility to display some content of a model. But it would be very inefficient for the view to query continuously the model to see what changed. So, MVC uses a push model: the view can query the model to display some content, but the model broadcast to the view that changes happened and that it's worth to consider refreshing the display.
Different views may show different content. Let's suppose that the model is the digital twin of a car. One view could just show the speed, as it would appear on a dashboard. The other view could ignore the speed, and just draw the car in 3D. To avoid that models has to know all the inner details of every possible kind of views, the broadcast is kept minimalistic.
The notify()
message is this exaclty this kind of minimalistic communication.
GoF popularized design patterns and started their book with decomposing the different features of MVC into different independent patterns. The subscribe/notify relationship between view and model was mapped to the observer pattern. Since their design were very suitable for strongly typed languages, the notify()
/update()
became very popular.
The pattern works as follows:
Observer
class. In many modern languages, however, the tendency is to replace the abstract class with an interface
.notify()
/update()
), without telling precisely what.Interfaces are like empty shells that hide what might be within. They are by themselves not sufficient to define a comprehensive solution; they are just a part of the puzzle:
interface
) or Swift (protocol
), once the interface defined, you need to defined classes that implement them.In both cases, having the interface allows the model and the view to understand each-other. But for an MVC broadcast to happen, the model needs to know whom to inform of changes, and the views have to know whom to listen to.
So you will end-up with something similar to the observer.
Now the traditional observer pattern is not the only possible solution. You could think of:
onSpeed()
, onShape()
). Views can react more efficiently. THe only problem is that views are forced to implement methods that they are absoluetly not interested in (i.e. no interface segregation)notify()
Upvotes: 1