Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC / Observer & Immutable Data Structures

Can You please explain what are analogues of MVC / Observer techniques in two cases:

  1. Immutable Objects (OOP style)
  2. 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?

like image 563
Alex Craft Avatar asked Aug 05 '26 14:08

Alex Craft


1 Answers

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.

like image 53
Alex Craft Avatar answered Aug 08 '26 15:08

Alex Craft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!