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

Design Patterns in Drupal 7

Design Patterns in Drupal 7

My talk on Software Engineering Design Patterns implemented in Drupal at Drupal Camp Singapore 2013.

Avatar for Amarnath Ravikumar

Amarnath Ravikumar

April 27, 2013

Other Decks in Programming

Transcript

  1. Motivation (2) An added benefit comes when the framework is

    documented with the design patterns it uses. People who know the patterns gain insight into the framework faster. Even people who don't know the patterns can benefit from the structure they lend to the framework's documentation. - Gang of Four, Elements of Reusable Object-oriented Software.
  2. Design Patterns could be.. Drupal-specific, re-usable ideas • Using drupal_static()

    • http://dropbucket.org/node/254 • Use confirm_form() on entity delete OR Generic, abstract ideas implemented in Drupal. A fresh look at things you already know & love.
  3. Design Patterns are.. Named ideas Focus on code-reuse & maintainability.

    Implementation-independent Free to discuss & compare implementations ◦ Cross-framework ice breaker? :) A way to evaluate a framework* Potential framework entry-point
  4. Design Patterns in Drupal.. Subtle, implicit • OOP misconception •

    Documentation Why should you know? • Architecting complex, custom modules • For kicks?
  5. Basic Ideas Loose coupling of components ◦ Scale. Models real

    world. Inversion of control Code Reuse / DRY Indirection • theme_button() v theme("button")
  6. Factory, Strategy patterns (1) Factory: Object builder Strategy: A particular

    solution to achieve a task. Eg. Mail system Cache backend Database connection
  7. Factory, Strategy patterns (2) Eg. (contd) MailSystemInterface DefaultMailSystem (core) DevelMailLog

    (devel) AmazonSESMailSystem (amazon_ses) DrupalCacheInterface DrupalDatabaseCache Redis_Cache
  8. Factory, Strategy patterns (3) Non-OOP can? Drupal's swappable session backend.

    Source code file (Object) Drupal core (Factory) Access modifiers (by Convention) Interface (by Convention)
  9. Observer, Mediator patterns (1) React to events in another component

    (subject) Listener/Handler/Callback/Subscriber In Drupal, we have: • Event Hooks Eg. hook_entity_(presave|insert|update|delete) • Triggers & Actions
  10. Observer, Mediator patterns (2) Subscription (Expressing intent) Typically, there is

    coupling between the observers and the subject but not between themselves. Drupal: Subject - Mediator* - Observers
  11. Observer, Mediator patterns (3) A protocol to govern the order

    in which observers will be notified independent of the subject. The protocol for an observer to express intent can vary independent of the subject. Can account for dependencies between observers (if there are)
  12. Dependency Injection Components declare dependencies. Dependency resolution offloaded to the

    framework. Eg. hook_menu() (Core), hook_services_resources() (Services)
  13. Chain of Responsibility (1) Delegate the responsibility of handling a

    request to a set of candidates instead of one, allowing fallback. Exception handling* Eg. Hook suggestions in the Drupal theme system.
  14. Decorator Adds additional behaviors/properties (enhancements) to an object at run-time.

    Drupal's QueryExtendableInterface Entity API's metadata wrapper object*
  15. Review 1. Basic Ideas 2. Design Patterns ◦ Factory, Strategy

    ▪ Object-oriented Design ◦ Observer, Mediator ◦ Dependency Injection ◦ Chain of Responsibility ◦ Decorator ◦ Adapter