Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Leveraging Rails

itchy
February 05, 2014

Leveraging Rails

Patterns to Enhance your MVC

itchy

February 05, 2014
Tweet

More Decks by itchy

Other Decks in Programming

Transcript

  1. ImageSet Null Object a Null Object is an object with

    defined neutral (‘null’) behavior.! ! ! ! — Wikipedia
  2. Address Remote Facade Provides a coarse-grained facade on fine-grained objects

    to improve efficiency over a network! ! ! — Martin Fowler
  3. Address street = ! city = ! zip = !

    street! city! zip AddressFacade address! update_attributes
  4. Description The most common use of this pattern is is

    between a presentation and a domain model…! Design an application without distribution, then layer the distribution ability on top of it with Remote Facades and Data Transfer Objects! — Martin Fowler
  5. Address Facade A facade is an object that provides a

    simplified interface to a larger body of code! ! ! — Wikipedia
  6. Data Model Facade Role Model Model View Web Controller Role

    Role ❖ display_value! ❖ display_name
  7. DCI & SOLID Your design task is to recognize that

    a role exists, define the interface of its duck type and provide an implementation of that interface for every possible player! — Sandi Metz
  8. Description ❖ DCI (Trygve Reenskaug, Jim Coplien)! ❖ Clean Ruby

    (Jim Gay)! ❖ SOLID! ❖ Uncle Bob! ❖ Sandi Metz
  9. Purpose The purpose of DCI is to produce code that

    a person can read and understand and so reason about its correctness! — Trygve Reenskaug
  10. Clean Ruby While simple models may support combining data and

    behavior in one class, doing so can lead to difficulty in responding to change later. The classes that we begin to define should represent data or model the behavior of the data object, but not both. — Jim Gay p. 24
  11. Clean Ruby This is where the ideas in DCI can

    help improve our architecture. In our example we have two accounts where one acts as the source and the other acts as the destination for the transfer of funds… The context is the object responsible for organizing the data and roles for carrying out your use cases. It’s a powerful model for encapsulating your business logic because it gives you a clear way to organize the interactions of objects in your system. ! — Jim Gay p. 37
  12. Implementation ❖ Write context as module and extend it on

    instances when they take on a role! ❖ Class methods vs instance methods! ❖ Composition & Duck Typing
  13. Composition & Duck Typing ❖ SOLID! ❖ Roles! ❖ http://mikepackdev.com/

    blog_posts/24-the-right-way- to-code-dci-in-ruby
  14. POODIR Composition describes a has-a relationship. Meals have appetizers, universities

    have departments, bicycles have parts. Meals, universities, and bicycles are composed objects. Appetizers, departments, and parts are roles. The composed object depends on the interface of the role.! — Sandi Metz
  15. POODIR When using composition, the natural tendency is to create

    many small objects that contain straightforward responsibilities that are accessible through clearly defined interfaces.! ! — Sandi Metz
  16. POODIR Because composed objects deal with their parts via an

    interface, adding a new kind of part is a simple matter of plugging in a new object that honors the interface.! ! — Sandi Metz
  17. POODIR At its best, composition results in applications built of

    simple, pluggable objects that are easy to extend and have a high tolerance for change.! ! — Sandi Metz
  18. WINNING Your design task is to recognize that a role

    exists, define the interface of its duck type and provide an implementation of that interface for every possible player! — Sandi Metz
  19. VS

  20. Takeaway ❖ Is vs Does! ❖ Context ! ❖ Roles!

    ❖ Smaller, Single Purpose Objects! ❖ http://orgsync.github.io/ active_interaction/
  21. ❖ http://blog.cfoedge.com/2012/07/31/leveraging-cfo-expertise/! ❖ http://licensing4profits.com/leveraging-your-ip-6-step-licensing-strategy/! ❖ https://skitch-img.s3.amazonaws.com/20111231-n9qswtxgswfmmb1qq1qgj6c75y.jpg! ❖ http://bobsegarini.files.wordpress.com/2011/11/greatness-bender.jpg! ❖ http://www.disneydreaming.com/wp-content/uploads/2011/02/Special-Agent-Oso.jpg!

    ❖ http://en.wikipedia.org/wiki/Null_Object_pattern! ❖ http://s3.amazonaws.com/data.tumblr.com/tumblr_kvh5fd9wn31qa3b4go1_r1_1280.jpg? AWSAccessKeyId=AKIAI6WLSGT7Y3ET7ADQ&Expires=1391543515&Signature=rs%2BA45hWTH17xVq1H1G%2B3fG9rPc%3D#_=_! ❖ http://en.wikipedia.org/wiki/Facade_pattern! ❖ http://www.evergreenata.org/best-rated-toilets-for-best-life-quality/ultra-flush-toilet/! ❖ http://humourisms.com/wp-content/uploads/2012/07/Zombie-identity-crisis-908x1024.jpg! ❖ http://0.tqn.com/d/gohawaii/1/7/8/N/4/uss_bowfin_017.jpg! ❖ http://www.chaoticneutral.me/wp-content/gallery/my-chaotic-gallery/jessica-josh-06361-220x135.jpg! ❖ http://static4.businessinsider.com/image/5254342cecad044955e390c2/peyton-manning-is-about-to-destroy-tom-bradys-single- season-touchdown-record.jpg! ❖ http://upload.wikimedia.org/wikipedia/commons/1/12/Coach_Jason_Garrett.jpg! ❖ http://echidnainc.com/blog/wp-content/uploads/2013/08/custom-made.png! ❖ http://dailybunny.org/2012/12/25/bunny-will-play-the-part-of-santa-if-you-leave-out-carrots-and-apples-for-him/! ❖ http://guardianlv.com/wp-content/uploads/2014/01/man.jpg