The pattern described in http://bost.ocks.org/mike/chart/ seems like a bit of an anti-pattern to me, at least when it comes to sharing extensible code with others (even within the same team). By design one cannot modify the internals of the closure. One can't even override the setters/getters externally because new getters/setters will be scoped to the point of their definition.As an example of the challenges the pattern poses here's some code from NVD3, a library that uses this pattern...(Contrived examples: I know the authors chose to make these private)
Let's say that I wanted to change the showTooltip method in the first link for just one of my charts, my only option is to copy the entire chart to a new js file and make my changes. But if this were developed with a classical OO pattern I would be able to subclass the chart and override the showTooltip method (at my own risk, of course).
Or let's say that I wanted to change the '1em' here. Again, I have to copy the entire file and modify it. If it was using the OO pattern and the highlighted code inside a renderTitle method I could subclass, override, and then call super.
With these examples one could argue that the authors have just been too aggressive about what is private (or that I am being to greedy in customization), but I would argue that private abuse (heh) is actually what is encouraged by this pattern and that when it occurs there is little recourse for the user.Here's another example. At my company we have attempted to create a base class for our force-directed network graphs. My team and I want subclass this to suit the needs of the implementation. (User-configurable physics, differences in visual representation of the nodes and edges, etc.) The reusable chart pattern breaks down pretty hard for us in this scenario of shared functionality.Has anyone encountered these difficulties? I'm interested in thoughts from the D3 community on this.PS - Thank you Mike and team for D3. It is amazing.PPS - I'm not trying to pick on NVD3. It is also amazing.
As others have said, the design does have ways to achieve the outcomes you're looking for, but perhaps the particular implementations you're looking at don't.
Customisation by inheritance is itself generally an antipattern. Composition etc generally get to a design that's easier to scale and maintain.
In your examples, those overrides would need to be part of the public API, or you'd find your code breaking at a later point. They could equally be part of the public API in the reusable charts design.
That said, I haven't been using the pattern in my charts; the other day I sat down to change some of them over, but I'm not sure if it suits my application. There's nothing that says you should use the pattern, I think it was an attempt to get some sort of consistency in the d3 ecosystem.
(I've been using CoffeeScript classes with a simple interface and a compositional design.)
Some of the libs could do with being more extensible; I don't much like the "do the chart and fix up the attributes you don't like" approach. But there's always forking and pull requests; that's the advantage of open source.
The pattern described in http://bost.ocks.org/mike/chart/ seems like a bit of an anti-pattern to me, at least when it comes to sharing extensible code with others (even within the same team). By design one cannot modify the internals of the closure. One can't even override the setters/getters externally because new getters/setters will be scoped to the point of their definition.
As an example of the challenges the pattern poses here's some code from NVD3, a library that uses this pattern...
(Contrived examples: I know the authors chose to make these private)
https://github.com/novus/nvd3/blob/master/src/models/bulletChart.js#L39-L45
Let's say that I wanted to change the showTooltip method in the first link for just one of my charts, my only option is to copy the entire chart to a new js file and make my changes. But if this were developed with a classical OO pattern I would be able to subclass the chart and override the showTooltip method (at my own risk, of course).
https://github.com/novus/nvd3/blob/master/src/models/bulletChart.js#L145-L155
Or let's say that I wanted to change the '1em' here. Again, I have to copy the entire file and modify it. If it was using the OO pattern and the highlighted code inside a renderTitle method I could subclass, override, and then call super.
With these examples one could argue that the authors have just been too aggressive about what is private (or that I am being to greedy in customization), but I would argue that private abuse (heh) is actually what is encouraged by this pattern and that when it occurs there is little recourse for the user.
Here's another example. At my company we have attempted to create a base class for our force-directed network graphs. My team and I want subclass this to suit the needs of the implementation. (User-configurable physics, differences in visual representation of the nodes and edges, etc.) The reusable chart pattern breaks down pretty hard for us in this scenario of shared functionality.
Has anyone encountered these difficulties? I'm interested in thoughts from the D3 community on this.
PS - Thank you Mike and team for D3. It is amazing.
PPS - I'm not trying to pick on NVD3. It is also amazing.