Wednesday, August 1, 2007

Working with Key-Value Pairs

The large application I work on uses an extensive plug-in architecture that allows us (or our clients) to radically change the behavior of the application quickly. In most of our situations, we know our concrete classes ahead of time because we extensively test them before releasing them into the wild. As a result, we use a Factory creational pattern that keeps the requestor class independent of the actual concrete class. Here's the classic example of this pattern (in Java):


public static final int IMAGE_JPG = 0;
public static final int IMAGE_GIF = 1;
public static final int IMAGE_PNG = 2;

Image createImage(int imageType) throws UnsupportedImageTypeException
{
Image newImage = null;
switch(imageType)
{
case IMAGE_JPG:
newImage = new JPGImage();
break;
case IMAGE_GIF:
newImage = new GIFImage();
break;
case IMAGE_PNG:
newImage = new PNGImage();
break;
}

if(null == newImage) throw new UnsupportedImageTypeException("Unknown type");

return newImage;
}


Nearly everyone of our plug-ins allows the customer to define their own parameters. For example, our application has the requirement to forward or relay data to the client's specified web server when certain events transpire. To get that information on a client-by-client basis, we rely on the client to define key-value pairs. In this arbitrary HTTP example, clients define:

  • authentication as {none, basic}

  • username

  • password

  • method as {POST, get}

  • url

  • etc.



Gathering these key-value pairs is simple, but early on we realized the value in creating a re-usable component to handle this situation. Since there isn't any real intellectual property in this component per se, I figured I'd share it with the blogosphere for all to use.

This component is driven by 3 classes:


  1. KeyValuePair.as - simple class that defines propertyName and propertyValue attributes.
  2. KeyValueCollection.as - class that extends the ArrayCollection class and implements a smart constructor and sorting routines that facilitate the easy management of key-value pairs.
  3. KeyValueScreen.mxml - this is the main UI for the component.


A functioning demo app and all the source code is available over here at my non-developed personal site (hey- there's only so many hours in the day, and blogging provides more value then me setting up an "I love me" web site, no??). As always, right click and choose "View Source" to see how things were written.

So here's a bit of an explanation of the components architecture. The KeyValuePair class should be self-explanatory. The KeyValueCollection class is responsible for managing the collection of key-value pairs. Its constructor is smart enough to iterate through the collection its provided and look for an either instance of KeyValuePair or instance of an Object that has the properties propertyName and propertyValue. In fact, the hasOwnProperty() method in ActionScript is a super easy way to do some basic reflection on unknown classes. This is a classic example of where a class may not be a concrete instance of KeyValuePair, yet the component utilizes reflection and can still make an informed decision as to the applicability of the object to participate in the collection. For those OO newbies, I'm describing effectively the difference between inclusional and operational polymorphism. Since a discussion on this would be off-topic, a quick google found this page here that quickly describes the differences.

The Flex manual is very explicit about how to effectively use the IViewCursor interface. First and foremost, the interface supports addition and removal of properties, but not updates per se. Next, the interface docs indicate that "the findAny() method can be somewhat more efficient than either the findFirst() or the findLast() method." By leveraging this knowledge, the class can quickly discern if the key already exists in the collection without having to manually iterate over what could be the whole collection.

Side Bar: I've not had the chance to really look at the sorting algorithm that ActionScript uses, but I can only assume that the gurus over at Adobe aren't implementing something that takes time proportional to N^2 to sort N randomly arranged items.

The collection is extended to include 2 new methods, insertOrUpate() and remove(). As the name implies, you can add or update an existing key-value pair with the first method, and altogether remove the key-value pair with the later method. By using the IViewCursor's findAny() method, we can quickly determine if an insert or update (delete-add, in this implementation) will take place.

The UI in the MXML file exposes a public property named propertyList that provides the requestor with access to the key-values that the user manipulated. The demo should be pretty clear as to how one can add or remove key-values from the UI. I've also skinned the add and remove buttons a la Mac OS X. The +/- metaphor is used extensively over here on my platform of choice, and it is fairly intuitive to pick up for newbies using the app.

I'm sure this component is suitable for many needs, but I actually had to strip a bunch of functionality out before posting it- so your mileage may vary (i.e. you may need to expand or add some more functionality). For example, our app is localized and so all of the labels and messages in my real-world component used ResourceBundle instances to manage those labels.

Enjoy!

No comments: