The mediator design pattern can help you achieve and maintain synchronization between a group of PHP classes. In this first of a two-part series, you'll be introduced to the mediator pattern, how it functions, and how it can help you with your application development.
As you'll possibly agree with me, at some point in your productive life as PHP developer, you've had to build web applications comprised of different, highly independent modules, which naturally perform well-differentiated tasks. Besides, it's normal to see that these modules very often establish a carefully planned interaction with each other, by means of a centralized mechanism, to stay in synchronization with the rest of the application.
Database interfaces and data validation systems, among other applications, are clear examples of using a unique, centralized programming module to keep all the different components that integrate a PHP application synchronized. You should be quite familiar with this concept.
The previous schema of interaction between the diverse modules of a given application seems to be pretty straightforward to implement during the development period; however, it should be admitted that this model isn't too easy to apply, particularly when it comes to using a set of predefined PHP classes.
The creation of a specific class, which has to be capable of keeping the interaction of other classes centralized (and synchronized) sounds hard to achieve, at least without using the benefits offered by pattern-based programming. So what's the point in utilizing this approach, after all?
Well, in this case, the synchronization between a group of PHP classes can be made by implementing a useful pattern widely known as "mediator." In short, when the mediator pattern is applied, there's a single class that implements the logic required to keep all the other classes completely synchronized with each other. This means that not only will all the modifications introduced by one class be reflected in the others, but that those changes will be performed via the interface of the mediator class.
Of course, it's quite possible that you find the above definition rather hard to grasp, therefore in this two-part series, I'm going to show you how a mediator class can be defined, and logically how it can be put to work as well, by employing numerous instructive examples.
So, are you ready to learn more about how to implement this handy design pattern with PHP 5? Okay, let's get started now!