24

The use of design patterns in programming is wide spread across many programming languages. A number of examples are the factory, or singleton design pattern. Many of these patterns use object orientation to create abstraction and encapsulation in the code, they aim at make code re-usable and structured. Many of these design patterns could also be used in R, maybe by using the proto library, or standard R object orientation?

My questions are:

  • What base code (S3, S4) / packages (proto, R.oo) can I use to reproduce design patterns as for example mentioned in the book by Gamma et al?
  • Are there examples of design patterns implemented in R, both in base R, or in packages?
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • 1
    The R.oo package provides OOP-like functionality in R (using S3 classes). It does not support the full range of OOP features and as such would not support the fulll range of OOP-based design patterns. I would also look at S4 classes http://www.stat.auckland.ac.nz/S-Workshop/Gentleman/S4Objects.pdf – Suraj Mar 01 '12 at 16:25
  • S4 classes look promising, although the manner of constructing objects looks a bit clunky in comparison to e.g. Python. – Paul Hiemstra Mar 01 '12 at 17:01
  • 1
    I've implemented a few design patterns sketched out in "Head First Design Patterns" using Reference classes. They map more closely to OO use in other languages than S4 methods, say, so involve less translation. – jverzani Mar 01 '12 at 17:04
  • Could you post a link to your work as an answer? Or if it is not already online, post some of the code as an answer? – Paul Hiemstra Mar 01 '12 at 17:09
  • 1
    Okay, I put an example of the Singleton Pattern here: https://gist.github.com/1953641 This was really a formal exercise and perhaps somewhat arcane for R users. The `memoise` package can do this much easier, though I did have a use case. The basic idea for the structure came from the Head First book. – jverzani Mar 01 '12 at 22:22
  • I'm sorry, but this question falls into the same line as the R/oop question [here](http://stackoverflow.com/questions/9521651/r-and-object-oriented-programming#question), it is too broad, and is not a good fit for the Q&A format on SO. – Lasse V. Karlsen Mar 01 '12 at 23:04

1 Answers1

5

Some examples of design patterns:

  • The system.time() function seems to behave much like a decorator pattern. However, almost exclusively decorators are mentioned in the context of object oriented programming. But still, it has the feel of a decorator, it extends (or decorates) an existing piece of code (in OOP always an object) with additional functionality without any need to change the piece of code. Here system.time() is shown in action:

    system.time(bla <- Sys.sleep(1000))
    
  • @jverzani posted an example of singleton pattern on github.

  • An example of a Strategy Design Pattern is the apply family of functions. The functionality of looping over the given object is generic, the function that is applied (the strategy) is chosen when the user supplies the function.
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149