3. Introduction and Terminology
• Coupling or dependency is the degree to
which each program module relies on
each one of the other modules
• Coupling measures the likelihood of a
change or fault in one module affecting
another module
4. Module Purpose/Behavior
• Module introduces new feature
• Module injects into existing functionality
• Module integrates with existing feature
• Module extends/overlaps existing
functionality
6. General Task of Injecting into Functionality
• Module A exists and already implements
the functionality
• Module B expands the Module A
• Module B should be triggered at the
specific point of the Module A execution
8. Conditional Call Implementation
Framework
Module A Module B
Call
• Module existence
checking
• Module B now
can be removed
• Module A still knows
about Module B
• Rename Module B –
change Module A
• Add new module –
change Module Aif (exists(‘Module B’)) {
call(‘Module B’);
}
Module C
9. Maintaining the List of Dependents
Framework
Module A Module B
Register
• It’s observer pattern
• Module B
relies on Module A
• New modules can
register within the
Module A without
changing anything
• Rename Module A –
change dependent
modulesModule C
Notify
Notify
Register
10. Publish/Subscribe Messaging Pattern
• Pub/sub – universal decoupling solution
– senders of messages do not program the
messages to be sent directly to specific
receivers
– subscribers express interest in messages
without knowledge of publishers
11. Low Modules Coupling Using Pub/Sub Pattern
Framework
Module A Module B
• Framework should
take care of coupling
• Modules don’t know
about each other
• Any module can be
removed
• New modules can
subscribe to existing
messages without
changing anythingModule C
Messages Manager
Publish
Subscribe
Call
Subscribe
Call
12. Magento Events
• Magento events – object-oriented
implementation of the pub/sub pattern
• Event dispatching calls – points of
integration
• Subscription through config.xml
13. Framework
Low Modules Coupling with Magento Events
Module A
Subscribe
Module B
Call
config.xml
Config Manager
config.xml
Merged config.xml
Events Manager
Mage::dispatchEvent
14. Achieved Coupling with Magento Events
Coupling depends on the implementation of the particular event handler
Coupling Type Description
Message coupling (low) Event doesn’t pass any data
Data coupling Simple event parameters, each is used
Stamp coupling Event data structure contains fields which may or
may not be used
Common coupling Usage of the global data registry; Accessing data
not only through methods
Content coupling (high) Not applicable to the pub/sub
15. Possible Improvements in Magento Events
• Strict event data types
• Convention over configuration
• Events naming convention
16. Strict Event Data Types
• Own event class for each unique
parameters combination
‒ Formal data structure declaration
‒ Access restriction to event data
‒ Events documentation auto-generation
17. Convention over Configuration
• Also known as coding by convention
• Decreasing the number of decisions that
developers need to make
• Simplicity while keeping flexibility
• Providing good defaults
18. Events Subscription in Configuration
• Mapping events to
handling routines
• Declaration of handlers
for each area
<events>
<catalog_product_load_after>
<observers>
<inventory>
<class>cataloginventory/observer</class>
<method>addInventoryData</method>
</inventory>
</observers>
</catalog_product_load_after>
class Mage_CatalogInventory_Model_Observer
{
public function addInventoryData($observer)
{
// ...
}
19. No Events Subscription in Configuration
• Good default mapping
• Single observer class
per module
• Method name equals
event name
<config>
<global>
<events/>
</global>
<frontend>
<events/>
</frontend>
<adminhtml>
<events/>
</adminhtml>
</config>
class Mage_CatalogInventory_Model_Observer
{
public function catalog_product_load_after($observer)
{
// ...
}
20. Event Naming Convention
Event Name Part Description
1. Module Name of the module, which introduces an event.
2. Layer Application layer where an event appears.
Allowed values: model, view, controller, service.
3. Entity Entity (domain) to which event has relation.
4. Action By default action should be equal to the method name,
which triggers an event.
[5. Suffix] Optional. Allowed values: before, after
lowerCamelCase event name to correspond to the method name
28. Recommendations
• Follow the low coupling principles – use
events to communicate between modules
• Extend only when no events are triggered
• Report requests for adding new events
• Don’t hesitate to trigger events in your
third-party modules and extensions
First of all, let me introduce myself.
My name is Sergey Shymko.
I’m the lead PHP developer in the Magento.
I started in Magento as a Core team member, then moved to the Support department.
And currently I participate in the Magento 2 project.
The new one hour we’re going to discuss very interesting and difficult topic.
It’s “How to develop low coupled modules with Magento”.
I don’t want to stop on explaining the importance of the topic. I’m sure all you already know that.
Usually “good software design” means exactly low coupling between the components of the system.
Low coupling makes software maintainable, allows to grow functionality, while keeping the development cost reasonable, and so on, and so on.
All you know that.
So, what is coupling, can anybody help me to answer this question?
Anyone?
I’m sure that all we have intuitive filling what is coupling.
But, to be more specific let’s clarify the terminology.
Since we’re talking about modules.
In general, what is the purpose of any module?
Module always ships the piece of functionality.
It can be the completely new feature that doesn’t interact with the other modules in the system.
Any combination of the items
Observer OOP pattern is a subset of the publish/subscribe messaging pattern
observers subscribe to the particular subjects
OOP implementation of pub/sub pattern is the single global event manager
Convention over Configuration
Convention over Configuration
Cron job scheduling through config.xml
Let’s summarize what we have learned during this hour.