decorator & presenter design pattern
DESCRIPTION
Decorator & Presenter Design PatternTRANSCRIPT
![Page 1: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/1.jpg)
DECORATOR & PRESENTERDESIGN PATTERN
@DonSchado | 21.05.2014
![Page 2: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/2.jpg)
WHY?
![Page 3: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/3.jpg)
Because your controllers become bloated and logic begins to creep into your views (templates)
![Page 4: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/4.jpg)
implement procedural helpers or adding view related bulk to your models?
Solutions?
![Page 5: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/5.jpg)
implement procedural helpers or adding view related bulk to your models?
Solutions?
helpers are shit making fat models even fatter? No class should be fat. Ever.
![Page 6: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/6.jpg)
Why are helpers shit?
http://nicksda.apotomo.de/2011/10/rails-misapprehensions-helpers-are-shit/
„all helpers are mixed into the view automatically (…) [and are] globally available“
„There is nothing wrong with having those little helpers in your view. What I don’t like is that they are called without an obvious receiver – they look and feel like functions. This is wrong.“
![Page 7: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/7.jpg)
Why are helpers shit?
http://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters
„No seriously, helpers suck“
„Why is it in Ruby that everything is an object, even integers, yet as soon as we need to format a date, we all turn into Dijkstra and bust out structured programming?“
![Page 8: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/8.jpg)
DECORATOR
![Page 9: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/9.jpg)
Decorator pattern (also known as wrapper) is a design pattern that allows to dynamically add behavior to an individual object.
can be used on it’s own or wrapped by decorator
each decorator HAS_A (wraps) a component (holds a reference)
(composition)(inheritance)
![Page 10: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/10.jpg)
The decorator wraps the model, and deals only with presentational concerns. In the controller, you decorate the article before handing it off to the view
![Page 11: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/11.jpg)
https://github.com/drapergem/draperDraper: View Models for Rails
Draper adds an object-oriented layer of presentation logic to your Rails application
![Page 12: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/12.jpg)
![Page 13: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/13.jpg)
![Page 14: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/14.jpg)
![Page 15: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/15.jpg)
PRESENTER
![Page 16: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/16.jpg)
A presenter is a special case of the "Decorator design pattern".
http://sokolmichael.com/posts/2012-01-14-model-view-controller-presenter-better-separation-of-concernMVCP - Better Separation of Concern
![Page 17: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/17.jpg)
http://blog.jayfields.com/2007/03/rails-presenter-pattern.html
Presenter was inspired by the various GUI patterns documented by Martin Fowler.
An architecture that uses the Presenter pattern provides view specific data as attributes of an instance of the Presenter. The Presenter's state is an aggregation of model and user entered data.
![Page 18: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/18.jpg)
![Page 19: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/19.jpg)
![Page 20: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/20.jpg)
7 Patterns to Refactor Fat ActiveRecord Modelshttp://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
Service Objects
Value Objects
Form Objects
Query Objects
View Objects
Policy Objects
Decorators
further reading:
![Page 21: Decorator & Presenter Design Pattern](https://reader034.vdocuments.us/reader034/viewer/2022042606/5486ff66b47959140d8b5364/html5/thumbnails/21.jpg)
thx! (and have fun refactoring all the fat classes)