In this morning’s meeting, we did a quick estimate on what environments the widgets we’re designing should live in, and determined that Facebook would be the primary target for launch, with the ability to add on more environments being key for the future.
In order to support multiple environments, the part of the widget that generates the embed tags people will use to put the widget on their blog/page/community will be very important. It needs to be able to send login and password information for instance to community APIs (like facebook) as part of the widget embedding process. Therefore we have a certain order of operations that should apply, something like this;
1. Set the base tag (all tags will have some things in common, such as the markup language)
2. Login (this will be optional to some tags, others will not need this step)
3. Assemble Tag (use the information returned by the Login step, combine it with the base tag from step one)
4. Return Tag (return the tag information to the object that asked for it)
Any time I see an order of operations like that, I immediately think “Template Pattern”. A template defines the order of elements in an algorithm, but delegates responsibility for the implementation of those elements to sub-classes.
In addition, a pattern very close to Template is the Factory pattern (many templates make use of a factory method in the subclass for producing objects unique to the sublcass of the template)
So firing up EA, I came up with an Abstract Factory that makes use of a Template for producing Embed Tags for the widgets. It looks like this;
As you can see, we now have an “abstract” class that defines the order of operations (template), but the details of that can be overridden by subclasses, that are the actual factories producing embed tags. It’s nice, clean, and very functional. The product produced by the factory is the Embed Tag itself (which is basically a string with some validataion built in).
-tim.

