Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of design patterns in R [closed]

Tags:

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?
like image 648
Paul Hiemstra Avatar asked Mar 01 '12 15:03

Paul Hiemstra


People also ask

When should design patterns not be used?

If a problem has two solutions, one that fits in ten lines of code, and another one with hundreds of lines of code along with a pattern, please consider not using the pattern. Their presence isn't a quality measurement.

What are design patterns useful for?

Why use a design pattern? The usefulness of using a design pattern is obvious. The design pattern can accelerate the development process. It provides proven development paradigms, which helps save time without having to reinvent patterns every time a problem arises.

Why is the Open Closed Principle so important in the decorator pattern?

The open-closed principle encourages extension over modification which is exactly what the decorator pattern does — if you realize that your existing component needs additional logic, the decorator pattern allows you to add this without modifying the original class in any way.

Are design patterns still used?

Quick answer: yes. Especially when you're at the beginning of your journey, design patterns are a good starting point. Even if you won't use them right away in your first projects, getting to know them will help you understand the existing solutions you're using. Complex solutions are made of patterns.


1 Answers

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.
like image 176
Paul Hiemstra Avatar answered Sep 20 '22 08:09

Paul Hiemstra