This pattern was used on the updated Sandlight Productions site. Check it out. (Now back to our regularly scheduled program.)
Sniffing along the Chain
While working on Learning PHP Design Patterns I build a sniffer to go with a CMS app that used an Observer pattern. At the time, I was thinking that I should really add a Chain of Responsibility (CoR) that could then send out content via the Observer pattern. In any event, I decided to go ahead with the project and post it on this blog.
To get started, take a look at the initial article on this blog explaining the Chain of Responsibility design pattern in PHP. The code in this application re-uses the bulk of the original one posted—remember that re-use is one of reasons to employ design patterns and this blog should certainly set examples where possible.
A Mobile Sniffer and a Chain of Choices
One thing we know for sure in the world of mobile devices is that more are sure to come. Once we make a nice big Web site that the client absolutely loves, along comes another mobile device.
“Ah, Joyce, could you add something so that the Bizarro 5000 can be detected?”
So don’t act like you didn’t know this was coming. That’s why the Chain of Responsibility is called into service. So we’ll add a class for the Bizarro 500 (Biz5000), link it to an appropriate Web page or make a new one, re-jigger the Client, and Bob’s your Uncle! Done and done.
Test the CoR Sniffer on your different devices to see the different results. If you have a device that you cannot connect, send in a comment and we’ll work out how to add your device to those sniffed out. Also, download the files for this post:
To get started, create a Handler abstract class. This will provide the abstract methods that give us lots of flexibility and loosely couple the classes. (It’s not much different than the original one created on this blog.)
< ?php abstract class Handler { protected $site; abstract public function handleRequest($request); abstract public function setSuccessor($nextService); } ?> |
The $site variable is one that will be used to instantiate one of three Web page builders. The two mobile sites (phone and tablet) will use jQuery Mobile, and the desktop site will be treated as a default site and uses no special code—just plain CSS and HTML. All of the sites are generated inside a PHP file using HEREDOC formatting.
Keeping in mind the the Chain of Responsibility design pattern is super simple to build, all we need are some concrete handlers and a Client. The Client participant in this pattern is hard-working, and a helper class, Request, has been added to help shovel requests to the concrete handlers. For this example, only four concrete handler classes have been created:
- Iphone
- Android
- Ipad
- Desktop
You may well ask, “What about Kindle Fire, Surface, and Blackberry?” And the answer is, “Add them yourself.” Keep in mind that design patterns are flexible, and it’s easy to add and change the parts.
Continue reading ‘Chain of Responsibility Mobile Sniffer’
Recent Comments