Can You please explain what are analogues of MVC / Observer techniques in two cases:
- Immutable Objects (OOP style)
- Immutable Data (functional style)
For example let’s consider following simple GUI example (You can try it live here http://tinkerbin.com/0XDHRXIl click ‘Run’ button to start it and wait 2 sec for text to appear)
It’s built with JavaScript because it’s easy to play and MVC / Observer are very natural to it
// Model containing the data.
var Post = Backbone.Model.extend({})
var PostView = Backbone.View.extend({
initialize: function() {
// Registering view rendering method as
// an observer on the model.
this.model.on('all', this.render.bind(this))
},
// Every time state of model changes
// this method will be called.
render: function() {
// Printing value of model.text attriubute.
this.$el.html(this.model.get('text'))
return this
}
})
// Now, any time the model is updated the view will be also
// automatically updated.
post.set({text: "hello, it's me"})
But I don’t quite understand how to do the same with Immutable OOP and Functional styles, what ways are there?
In case of classical MVC and OOP techniques there are implicit identifier for every object – its reference. Observer relies on this reference/identifier to dispatch messages to correct objects.
In Immutable world reference doesn’t identify object anymore (there may be multiple references for different versions of object) and in functional world there’s no objects at all. So, we need to explicitly supply the object identity.
The analogue of Observer in Immutable/Functional world is a Pub/Sub with explicitly provided object IDs.