Introduction and Terminology
Module Purpose/Behavior
General Task of Injecting into Functionality
Hard-Coded Call Implementation

 Framework
                                            •   Module A highly relies on
                                                Module B
                                            •   Module B
                                                can’t be removed


        Module A                 Module B




                      Call
Conditional Call Implementation

 Framework
                                              •   Module existence checking
                                              •   Module B now
                                                  can be removed
                                              •   Module A still knows about
                                                  Module B
          Module A                 Module B   •   Rename Module B –
                                                  change Module A
                                              •   Add new module – change
                            Call                  Module A



                                   Module C
 if (exists(‘Module B’)) {
            call(‘Module B’);
 }
Maintaining the List of Dependents

 Framework
                                                  •   It’s observer pattern
                                                  •   Module B
                                                      relies on Module A
                                                  •   New modules can register
                                                      within the Module A without
        Module A                       Module B       changing anything
                            Register              •   Rename Module A –
                                                      change dependent
                                                      modules
                   Notify

                                       Module C
         Notify


                            Register
Publish/Subscribe Messaging Pattern
Low Modules Coupling Using Pub/Sub Pattern

 Framework
                                                                    •   Framework should take
                   Messages Manager
                                                                        care of coupling
                                                                    •   Modules don’t know about
                                                                        each other




                                 Subscribe




                                                        Subscribe
                                                                    •   Any module can be


                    Publish
        Module A                             Module B                   removed
                                                                    •   New modules can
                                                                        subscribe to existing
                                                                        messages without
                                                                        changing anything
                              Call

                                             Module C

                              Call
Magento Events
Low Modules Coupling with Magento Events


        Framework
                                 Merged config.xml       Config Manager


                                     Events Manager




                                                                          Subscribe
                    Module A                          Module B

                    config.xml                        config.xml

                                              Call
               Mage::dispatchEvent
Achieved Coupling with Events
 Actual coupling depends on the implementation of the
 particular event handler:
   Message coupling (low)
   Data coupling
   Stamp coupling
   Common coupling
   …
   Content coupling (high)
Events Subscription in Configuration

 <events>
     <catalog_product_load_after>                            •   Mapping events to handling
          <observers>                                            routines
              <inventory>
                                                             •   Declaration of handlers
                  <class>cataloginventory/observer</class>
                  <method>addInventoryData</method>
                                                                 for each application area
              </inventory>
          </observers>
     </catalog_product_load_after>




 class Mage_CatalogInventory_Model_Observer
 {
     public function addInventoryData($observer)
     {
         // ...
     }
No Events Subscription in Configuration

 <config>
         <global>                                            •   Good default mapping
                 <events/>                                   •   Single observer class per
         </global>
                                                                 module
         <frontend>
                 <events/>                                   •   Method name equals event
         </frontend>                                             name
         <adminhtml>
                 <events/>
         </adminhtml>
 </config>

 class Mage_CatalogInventory_Model_Observer
 {
     public function catalog_product_load_after($observer)
     {
         // ...
     }
High Modules Coupling


        Framework




                    Mage_Cron                            My_Module
                                   Schedule Job




                                Get Schedule & Run Job


           Removing Mage_Cron breaks My_Module
Low Modules Coupling Using Configuration


         Framework
                                  Merged config.xml      Config Manager




                                       Get Schedule




                                                                          Schedule Job
                     Mage_Cron                        My_Module

                     config.xml                       config.xml



                                      Run Job


         Removing Mage_Cron doesn’t break anything
sergey.shymko@magento.com

Developing loosely coupled modules with Magento

  • 3.
  • 4.
  • 6.
    General Task ofInjecting into Functionality
  • 7.
    Hard-Coded Call Implementation Framework • Module A highly relies on Module B • Module B can’t be removed Module A Module B Call
  • 8.
    Conditional Call Implementation Framework • Module existence checking • Module B now can be removed • Module A still knows about Module B Module A Module B • Rename Module B – change Module A • Add new module – change Call Module A Module C if (exists(‘Module B’)) { call(‘Module B’); }
  • 9.
    Maintaining the Listof Dependents Framework • It’s observer pattern • Module B relies on Module A • New modules can register within the Module A without Module A Module B changing anything Register • Rename Module A – change dependent modules Notify Module C Notify Register
  • 10.
  • 11.
    Low Modules CouplingUsing Pub/Sub Pattern Framework • Framework should take Messages Manager care of coupling • Modules don’t know about each other Subscribe Subscribe • Any module can be Publish Module A Module B removed • New modules can subscribe to existing messages without changing anything Call Module C Call
  • 12.
  • 13.
    Low Modules Couplingwith Magento Events Framework Merged config.xml Config Manager Events Manager Subscribe Module A Module B config.xml config.xml Call Mage::dispatchEvent
  • 14.
    Achieved Coupling withEvents Actual coupling depends on the implementation of the particular event handler: Message coupling (low) Data coupling Stamp coupling Common coupling … Content coupling (high)
  • 18.
    Events Subscription inConfiguration <events> <catalog_product_load_after> • Mapping events to handling <observers> routines <inventory> • Declaration of handlers <class>cataloginventory/observer</class> <method>addInventoryData</method> for each application area </inventory> </observers> </catalog_product_load_after> class Mage_CatalogInventory_Model_Observer { public function addInventoryData($observer) { // ... }
  • 19.
    No Events Subscriptionin Configuration <config> <global> • Good default mapping <events/> • Single observer class per </global> module <frontend> <events/> • Method name equals event </frontend> name <adminhtml> <events/> </adminhtml> </config> class Mage_CatalogInventory_Model_Observer { public function catalog_product_load_after($observer) { // ... }
  • 23.
    High Modules Coupling Framework Mage_Cron My_Module Schedule Job Get Schedule & Run Job Removing Mage_Cron breaks My_Module
  • 25.
    Low Modules CouplingUsing Configuration Framework Merged config.xml Config Manager Get Schedule Schedule Job Mage_Cron My_Module config.xml config.xml Run Job Removing Mage_Cron doesn’t break anything
  • 29.

Editor's Notes

  • #11 Observer OOP pattern is a subset of the publish/subscribe messaging patternobservers subscribe to the particular subjects
  • #13 OOP implementation of pub/sub pattern is the single global event manager
  • #19 Convention over Configuration
  • #20 Convention over Configuration
  • #26 Cron job scheduling through config.xml
  • #28 Let’s summarize what we have learned during this hour.