Recent Weblogs

Links I like

Super and Sub

A pattern is born

Super and Sub is a design pattern that has evolved in my code over numerous ajax applications. The generic view of super and sub is as such, super is the factory that handles the retrieval of data sets that the sub class will receive and build from.

Handle a Feed

A good implementation of Super and Sub is to look at a data feed, such as RSS. Super would retrieve the entire feed, parse the top level elements and then gather the set of items and send each item to a new instantiation of its Sub and work from there.

A more concrete example

ExploraSaurus is built using this pattern, ExploraSaurus.Super handles the ajax request for retrieving directory information and ExploraSaurus.Sub parses that response into an HTML Object and handles the events etc.

The benefit

If we take a good look at super, what is it? Its a factory class, its purpose is to handle top level functions such as data retrieval from the server and placement of objects within the dom, managing collections of subs etc. The Sub's responsibilities are encapsulated to creating DOM objects within its internal "root" element, events that occur within itself and micro tasks with the server such as an update or deletion of the record it is representing.

With Super being the gateway for all of the major data requests it is best to extend your super from a Base class, such as Ajax.Application.Base which provides a great platform for ajax applications to be built from, allowing central functions in which all data is sent and received allows easy data massages as its passed to and from the server.

Comments are Disabled.