Magento has a pretty well implemented Event/Observer system, but before we dive into the specifics, let’s make sure that we have a clear understanding of the design patterns that are in play in the Magento application.
Specifically, lets’ talk about the concept of the event system, how it works and what it is. Though we’ve covered certain design patterns in other articles on Tuts+, we haven’t done so within the context of Magento.
To that end, let’s take a look at the pattern in order to gain a clear understanding of how it is, how it works, and how it’s beneficial.
Design Patterns as a Whole
Over the years a wide array of design patterns have been dveloped. But what is a design pattern exactly?
To quote Wikpedia:
“In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.”
If we reduce that a little we’re left with: a design pattern is a reusable solution to a common problem. That is as perfect a description.
The design pattern is a template that can be used to create a solution that will fit problems of the same nature. The pattern itself is not the actual solution, it defines the way the solution can be implemented as a guideline.
Your actual solution, even if adhering to the defined pattern can be different from mine as long as they both follow the guidelines set out by the design pattern.
The Observer Pattern
Now that we know what a design pattern really is, lets take a look at the Observer Pattern.
First implemented in the MVC architecture, the Observer Pattern has turned out to be a very valuable addition to the architecture. In part because of this an enormous amount of libraries have implemented the pattern over time and if you’ve been programming for a while it’s not unlikely you’ve already used it.
Enough history now though, let’s take a look at how it works.
To observe something is to look at it, or, to observe its state. In a technical context, this is no different. Observers by definition are watching something and in our case, events. The idea here is that if an event gets dispatched, the observers that are observing this event get executed.
Generally, the subject that is being observed will be passed along by reference in order for the observer to be able to update or check it for changes.
Check out this example put together from the Magento code:
Alright, so this is the validate method as its available in your run of the mill Magento installation. For clarity I replaced two variables with their actual values. You can see two events are dispatched here.
catalog_product_validate_after, each of these gives the product object along with the dispatch so that the observer might do something with it.
And here’s an observer class and its method. This class and method are hooked into the system using Magento’s configuration system. The event itself is handed to the observer class and the product is reachable from the event.
I’ll cover the actual creating and hooking of observers as well as reaching the subjects with more detail in the next post.
Why Use This Pattern?
One of the primary arguments to use the Observer Pattern, or its close relatives like the Pub/Sub Pattern is the modularity it provides.
Because anything can hook into an event that is being dispatched, you never have a strong reference to it. This is exceptionally useful in a setting where you’re always working with modules that may or may not provide services to a specific element that may or may not be available.
To use Magento again, I can build a module that will observe product changes as well as a module I built that fires off its own custom events. Now, in a regular system this module would be dependent on the earlier module, but using a Pub/Sub or the Observer Pattern I can make that dependency optional and only apply whatever it is my current module to it when it’s available.
After this quick refresher on what exactly design patterns are as well as the more in depth look at the Observer Pattern, in the next post I’ll explain how it’s used in Magento, how you can start using it in your own modules and provide them in your own modules as well to hook into from different ones, or for others to hook into.