Monthly Archive for December, 2012

Chain of Responsibility Mobile Sniffer

snifferUpdate!

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:

PlayDownload

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:

  1. Iphone
  2. Android
  3. Ipad
  4. 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’

‘Twas The Night Before OOPSLA

The annual meeting of the world’s most influential and best programmers can be found at OOPSLA (Object-Oriented Programming, Systems, Languages & Applications.) In the early 1990s, it was at OOPSLA meetings that the Gang of Four first began spooling out the design patterns that became the core of their book, Design Patterns. Ever since I began going to OOPSLA meetings in 2006, I’ve been able to slip in some of the internet languages like PHP and ActionScript 3.0. I first posted this thinly-disguised version of the Night Before Christmas a couple of years ago and have decided to make it an annual event. So here it is with a few tweaks:

Continue reading ‘‘Twas The Night Before OOPSLA’