125

There is all sorts of talk lately in the Ruby on Rails community about decorators and presenters.

What is the essential difference between the two? If there is, what are the clues that tell me which one to use over the other? Or perhaps to use the two in conjunction?

2 Answers 2

117

A decorator is more of a "let's add some functionality to this entity". A presenter is more of a "let's build a bridge between the model/backend and view". The presenter pattern has several interpretations.

Decorators are generic/general purpose. Presenters have a narrower range of responsibilities/uses. Decorators are used across domains, presenters are almost always related to view-like functionality.

10
  • 3
    Thanks. Seems like that Draper gem is a hybrid of presenter and decorator.
    – keruilin
    Commented Oct 22, 2011 at 15:45
  • 18
    @keruilin One thing to keep in mind: Decorators should really be able to decorate other decorators (as well as decorating the component object), because one of their purposes is to get around the limitations of inheritance. (Draper does not do this). The decorator pattern is very similar to the composite pattern in that sense, except handled from outside-in instead of inside-out (if that makes sense).
    – smudge
    Commented Dec 20, 2011 at 7:32
  • 9
    I see a decorator as a general purpose pattern, and presenter as a specific application of decorator related to the view layer.
    – Kris
    Commented Jul 13, 2012 at 9:40
  • 2
    @Smudge, draper decorators can decorate other decorates, at least as if the underlying models have an STI relationship.
    – keruilin
    Commented Sep 3, 2012 at 2:54
  • It seems that now Draper identifies itself as presentation layer wrapper - so it's no longer a decorator, but a presenter actually. From their GH: "Draper adds an object-oriented layer of presentation logic to your Rails application."
    – Jared
    Commented Sep 3, 2019 at 14:01
49

I suggest you to check this - Exhibit vs Presenter.

Decorator is a design pattern that is used to extend the functionality of a specific object by wrapping it, without effecting other instances of that object. In general, the decorator pattern is an example of the open/close principle (the class is closed for modifications, but available for extensions).

Both the exhibit and presenter patterns are a kind of the decorator pattern.

4
  • 1
    +1 for linking to that blog post by Mike Pack. Excellent post that explains the differences between the patterns.
    – ki4jnq
    Commented Feb 28, 2016 at 0:32
  • 1
    +1 for mentioning the Exhibit pattern. I ended up getting Avdi Grimm's book that explains it. Although, it wasn't the right solution for my problem it's still an amazing pattern. Excellent food for thought.
    – Yonk
    Commented Nov 10, 2016 at 23:59
  • Mike Pack's blog does not exist anymore, I'll try to find an alternative article or take it from archive.org Commented Feb 4, 2023 at 15:27
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.