You are hereHome / Development / Randomized Blogroll Plugin 0.2 released

Randomized Blogroll Plugin 0.2 released


By Gerd Riesselmann - Posted on 13 January 2005

I just released version 0.2 of my Randomized Blogroll WordPress plugin. You can download it here. I added customizable output as the only new feature, but did some fundemantal changes in the overall design.

Following I will explain some of the design decisions I made. If you are interested, read on.

I did two major changes:

  1. Replace arrays with a collection class.
  2. Introduce so-called Formatters implementing a strategy design pattern.

Compared with a dedicated collection class, an array has several disadvantages. First, all handling is left to the client. Code retrieving values from the array must know about the array's structure. Is it an associative array using keys? If so, what are the keys? Or is the array simply indexed? If so, is it zero-based or one-based? Client code should not bother about such problems, since these are implementation details. Even worse, client code is now tightly connected to the choosen data structure, which makes changing it a pain. This is the second drawback of arrays.

Introducing a collection class solves these problems. Managing the underlying array is done by the collection hidden effectively from the clients. Changing from an indexed to an associative array is easy - and that's exactly what I did, since I wanted a possibility to handle duplicated entries.

Have a look at the source to see the implementation. The collection class is called "rbr_LinkItems".

One may argue, though, this is overkill for a little plugin solving just one quite simple task. I agree. However, having a representation for an item in a list of links and having a custom collection class to combine them offers much more possibilities. Actually, once the collection is properly filled, we can do anything with it. And filling the collection is as easy as can be, making it a snap to implement support for sources other then OPML. Have a look at the function "rbr_rewriteCache" to see, how little code it takes to do the main processing.

The second main design change affects the output. While this was hard coded in the previous version it can easily be changed now. This is achieved using so-called Formatters. Formatters are an implementation of the strategy design pattern. I talked about using this pattern in two former articles, regarding page and table generation.

The trick is simple. Generating the output is delegated to an instance of a specifc class, and changing the instance changes the behavior. While the plugin currently just generates plain links, anyone can provide a customized Formatter class that for example displays the link's description or adds a RSS subsription to the list (the link items now keep this data, too). See the class "rbr_DefaultFormatter" for a simple reference implementation.

The design can be improved of course, and the current state is just a snapshot. However, I have a flexible data structure in the middle, so input can be extended easily. And the output can already be adopted to specific needs. This situation is quite comfortable, so the wishlist is open.