![]() |
Interfaces Revisited
After my last article on interfaces a few people said it would have been clearer if I had given a better example. I decided to write this tutorial/article to further explain the use of interfaces (and to give a decent example).
The code in this article will solve the following real life problem that I recently had to solve: I needed a system that allowed me to send emails to different combinations of groups of users. There were many different variations of "user groups" and so I needed a flexible system to allow me to easily send emails to different combinations of these groups and to also allow me to easily add, delete and change groups at a later date. I decided I needed one core class (which I named GroupEmailer) and one interface (which I namded RecipientGroup). I will later create classes that implement that interface. The class, GroupEmailer, is responsible for sending a specific email to a group of recipients. It must know which group of users we want to send emails to. I've left out the implementation of sending the actual email as I don't feel it is important to the example. PHP Code:
You can probably imagine how this will work. We'll add "groups" to the class (using addGroup()) which represents the group of users that we want to email, then we'll call the send() method to send the emails to each member of the groups. However, we've not actually got any code for returning the groups yet. You may have noticed that the addGroup() method is expecting an object that implements the RecipientGroup interface, this means that we need to create some classes that implement this interface. Before we do that, we need to create our interface: PHP Code:
Now that we have an interface to design our groups to we can start creating our group classes. We'll create two classes for this example, one for retrieving a list of admin email addresses and one for retrieving a list of member email addresses. Here's the class for retrieving the list of admin email addresses: PHP Code:
Here's the class for retrieving the list of member email addresses. This is almost exactly the same as the previous class PHP Code:
PHP Code:
Code:
karl@talkphp.comPHP Code:
Code:
karl@talkphp.com |
Really nice and clean mate, easy to read and definitely helps get a grasp on interfaces!
Can you think of another reason to use interfaces alike you have? I'm finding it hard to think of a good use for something similar to the: PHP Code:
|
In a nutshell it saves you having to go back to modify a class. Instead you can create another class and then include it in the construct's argument. Keep in mind that although you have a little repetition of code, all that should really be a repetition is the way you handle the MySQL queries, though this should as well be used via a MySQL class which makes querying simple.
|
bluesaga, another good example of this pattern can be found in the Zend Framework (in fact, they've used this kind of pattern throughout). If you look at the Zend_Valdiate class you can see that it can be used just like the class I built, it allows you to mix and match different validations to create your own valdiation group, here's some example code from the Zend Framework Reference Manual.
PHP Code:
|
Ah right, yea thats a great example! I see now, i take it addValidator makes an array of the classes, and then "doValidate" (or something similar) loops through those, checking for a exception and returning the exception if it finds one?
I can see exactly why that works, and how its a great thing :) One thing though, what happens if say you have 20-30 different string to validate. Surely this method would be a little slow? (Need different instances of Zend_Validate for each different string format?) |
yes it would be a little slow. Unfortunately you have to give up some efficiency for the flexibility of such a system. The same can be said with a lot of agile design patterns, they allow far superior flexibility but unfortunately you've got to model a lot of the data using objects, which can hinder the performance of your system :(
With that said, there are many ways to improve the performance of your PHP applications. I dont just mean small performance boosts either, you can increase performance over 50% by just using a compiler cache. |
| All times are GMT. The time now is 01:11 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0