[Rd] Reference classes: opinion on OOP design
janko.thyson at ku-eichstaett.de
Wed Nov 17 19:55:43 CET 2010
I'm aware that this post does not really comply with the posting guide with
respect to "providing a minimal reproducible code example". But I do hope
it's okay that I spared uninterested readers by putting that into the
The thing is that I'm not really familiar with "true" OOP as R is my first
programming language. So I wanted to ask kindly for some expert opinion on a
certain paradigm I've come up with concerning my use of class defs and
methods (S4 Reference Classes and S4 methods).
I thought it'd be neat to have Reference Class instances that can
- load data from remote places (e.g. a db, file whatever) on demand
- enable field access in a static OR dynamic way:
o static -> regular "static" field access (or if not initialized yet:
retrieve data (optionally buffer it) -> write it to "static" field)
o dynamic -> retrieve data (as it might have changed in the remote
location in the meantime) and optionally buffer it for later use
- enable sync of dynamic/buffer values with static field values (e.g.
whenever the difference between static and dynamic value passes threshold x
sync static and dynamic values.
- Provide a value history (which might be useful for something ;-))
- Enable explicitly setting field values and facilitate their "writeback" to
the remote location
The instances should be flexible enough to work in an "static only" and
"dynamic only" context and should make as much use of existing functionality
(e.g. S4 classes, S4 method dispatch etc.) as possible. I do want to have
the choice if I'm carrying (possibly loads of) data with me in the fields of
my object or if I compute/get them based on some function whenever I
actually need it. For example, I'm thinking about parameter estimates that
could automatically be reestimated based on rules that take into account a
constantly changing data structure (new observations come it quite
frequently or something like that).
What do you think of the way I've implemented this? Does this make sense to
you or is something like this done in another (and probably more elegant
THE HOMEWORK I DID
- I've looked at active bindings which are nice. But this limits me to funs
with either none or one argument if I'm not mistaken.
- I do like the idea of 'getRefClass("Blabla")$accessors(.)' setting up
get/set methods for me, but would need to customize them to my specific
(e.g. handling the default values of method arguments etc.)
I've attached a code example and would really appreciate your comments if
you find some time.
Thanks a lot!!
More information about the R-devel