events vs reference sharing

Suppose I have a TabManager class which holds tabs array. There is also a controller that needs to track this tabs property and draw tabs in the array. I have two approaches to sharing this data:

First approach is reference sharing:

controller.tabs = tabManager.tabs

There are also many variations of the statement above, for example, this one:

// getTabs() returns reference to private _tabs property
controller.tabs = tabManager.getTabs();

Second approach is to notify controller when tabs array changes:

tabManager.on("tabSetChange", function(tabs) {
   // tabs here are clone of tabManager.tabs, not the same reference
   // so modifying controller.tabs won't affect tabManager.tabs
   controller.tabs = tabs;

Is there are a some rule of a thumb to decide which approach to choose? Lately, I’m inclined to use second approach to prevent accidental data change and with this approach debugging seems easier. But the second approach is also much more elaborate.

Source: php

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.