Thanks to the pattern's benefits, it’s possible to implement it in PHP and develop applications that can be extended without having to appeal directly to Inheritance. In keeping with this idea, in preceding parts of this series I demonstrated how to take advantage of the pattern’s functionality and create a couple of “pluggable” applications. The first of these applications was pretty contrived, as it could only display different types of “renderable” objects on screen, including a few HTML widgets and some JavaScript boxes. The second one, however, presented a much more realistic scenario, since it could cache data by using either a server-side cache backend (which was a basic APC wrapper) or a client-side one, in this case composed of a simple proxy for the local storage feature packaged with HTML5. With these cache backends already up and running, the next (and last) step we must take is to put them to work in a concrete example. In this way you'll see how easy it is to swap them at runtime, in accordance with the specific needs of client code. So, in this final chapter of the series I’m going to set up the example, thus finishing this humble educational journal through the “Plug-in” pattern in PHP. Review: the previous cache system Since my goal here is to demonstrate that the caching system developed in previous tutorials of the series is really capable of plugging into different cache back-ends without having to modify the client code that consumes it, it’d be helpful to review the classes (and the interface) that make up the system. This way, you can recall the role that each of them plays in this context. Having said that, first here’s the interface that defines the contract that must be implemented by any concrete cache backend: (Cache/Cacheable.php) <?php namespace Cache; interface Cacheable As you’ll surely agree, the methods declared by the “Cacheable” interface are extremely intuitive, right? So move on and look at the following two concrete classes, which implement the two cache back-ends mentioned in the introduction. Here they are: (Cache/ApcCache.php) <?php namespace Cache; class ApcCache implements Cacheable
(Cache/ApcCacheException.php) <?php namespace Cache; class ApcCacheException extends Exception{}
(Cache/LocalCache.php) <?php namespace Cache; class LocalCache implements Cacheable
(Cache/LocalCacheException.php) <?php namespace Cache; class LocalCacheException extends Exception{} Although the above cache classes have already been discussed in depth, it’s worth briefly describing what they do, for the sake of completeness. The first one caches opcode via the popular APC PHP extension, while the second one is nothing but a simple wrapper for the local storage mechanism provided by HTML5. Now, do you remember how these cache backends do their thing? Good. Finally, here’s the client class that consumes the existing back-ends through a dead simple API. Check it out: (Cache/CacheHandler.php) <?php namespace Cache; class CacheHandler If someone asked for my opinion of the above “CacheHandler” class, I’d have to say that its implementation isn’t very impressive. However, first impressions are not always a programmer’s best friends. But why? Well, if you look more closely into the class, you’ll realize that it accepts an object of type “Cacheable” through its constructor, and that this object is stored as a protected property. Even though this doesn’t seem to a big thing, this is in fact the nitty-gritty of the “Plug-in” pattern! Effectively, by taking any “cacheable” object, the class can be easily fed multiple cache backends, aside from the ones defined above. This means it offers a flexible schema that can be easily expanded without modifying its original implementation. Of course, the best way to prove that this sample cache system is that flexible is by example. To fit this requirement, in the following section I’m going to create one, which will make use of the earlier “Apc” class to cache some trivial data in the server. To see how this example will be created, click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|