Always Get Better

Archive for February, 2009

Implementing Lazy Load Using a Proxy Class

Saturday, February 28th, 2009

Lazy load is a design pattern wherein an object is not instantiated until the last possible minute. This is very handy when working with lists of items whose contents are expensive to retrieve from the data store.

There are typically three ways of implementing lazy load:
1. Lazy Initialization – The object is set to null and checked/loaded when data is needed
2. Proxy – A proxy class for the object is created using the same interface as the original class; whenever a property is called, the proxy creates the object and returns the correct data
3. Ghost – The class loads only a partial set of its information until more is needed

Example Situation

In my example situation, we handling a catalogue of artists owned by a fictional record label. For each artist, we will store a name, musical genre, web site address, and number of albums.

UML for Artist Class

UML for Artist Class

Let’s pretend we have thousands of artists on our roster, and we need to print a catalogue containing all of their information. Rather than loading all of that data into memory right away and having to wait until that process is done before we can begin printing, it makes more sense to get a list of how many artists will be printed (so our software knows how many pages to print) but to only load the actual information when we are ready to print it.

The solution is to create an ArtistProxy class. ArtistProxy has an _artist variable who is set to null when it is initialized. Whenever we try to access the artist’s name, web site, etc from ArtistProxy, the class creates an Artist (only if not already done) and returns the property from _artist.

Our print function is never aware of ArtistProxy – as far as it is concerned it only ever deals with Artist. We accomplish this by creating an interface – IArtist – which acts as a contract for both ArtistProxy and Artist. If we add more properties to Artist later on, IArtist will keep us honest by forcing us to also update ArtistProxy.

Artist, ArtistProxy, and IArtist UML Diagram

Artist, ArtistProxy, and IArtist UML Diagram

Now that we understand how our classes relate to each other, it’s time to use them in code:

[source:csharp]
// Our printArtists() function looks something like this.
// Notice how we are unaware whether the artist is
// an actual object, or a whether it is a proxy.
public function printArtists( IArtist [] artistList )
{
foreach ( IArtist artist in artistList )
{
printOneArtist( artist );
}
}

// Implementation of the IArtist interface
public interface IArtist
{
string Name { get; set; }
string Genre { get; set; }
string Website { get; set; }
int getNumberOfAlbums();
}

// Implementation of Artist class
public class Artist : IArtist
{
private int _id;
private string _name;
private string _genre;
private string _website;

public Artist( id )
{
_id = id;
}

public string Name
{
get { return _name; }
set { _name = value; }
}

public string Genre
{
get { return _genre; }
set { _genre = value; }
}

public string Website
{
get { return _website; }
set { _website = value; }
}

public int getNumberOfAlbums()
{
return fictionalDataConnection->getNumberOfAlbums( _id );
}
}

// Implementation of ArtistProxy class
public class ArtistProxy : IArtist
{
private Artist _artist;
private int _id;

public ArtistProxy( id )
{
_artist = null;
_id = id;
}

public string Name
{
get
{
if ( null == _artist ) _artist = new Artist( _id );
return _artist.Name;
}
set
{
if ( null == _artist ) _artist = new Artist( _id );
_artist.Name = value;
}
}

public string Genre
{
get
{
if ( null == _artist ) _artist = new Artist( _id );
return _artist.Genre;
}
set
{
if ( null == _artist ) _artist = new Artist( _id );
_artist.Genre = value;
}
}

public string Website
{
get
{
if ( null == _artist ) _artist = new Artist( _id );
return _artist.Website;
}
set
{
if ( null == _artist ) _artist = new Artist( _id );
_artist.Website = value;
}
}

public int getNumberOfAlbums()
{
if ( null == _artist ) _artist = new Artist( _id );
return _artist.getNumberOfAlbums();
}
}
[/source]

Of course for the sake of convenience a few things are missing from my example:
1. An actual data source
2. Delegates (presumably one would include a ‘LoadArtist’ delegate so the proxy will be able to pass the actual loading of its artist to the data layer)

Facebook, Privacy and Drunken Photos

Wednesday, February 11th, 2009

As a user of Facebook (not to mention a longtime net citizen) I am well aware of the slimy feeling one gets when their image is bared for all to see. The owned application comes to mind first – strangers ‘bidding’ for your uploaded photos on Facebook, with the owner of the ‘purchased’ photograph receiving an obnoxious “you’ve been bought by X” email. I haven’t posted any compromising photos of myself (that would be silly) but the thought of my image being a component of someone’s “collection” is creepy to me.

Enter sites like YoBusted; members post photos and tag for the public to see – if you are the subject of a particularly hilarious (read: embarrassing) photo, you can remove it by signing up to a $20/month membership. Although some have suggested that YoBusted’s business model is nothing more than simple extortion, proving that claim in a court would be difficult – and what’s to stop a copycat site from doing the same thing (they probably already are).

The solution: guard your personal data. Don’t fill your Facebook profile with personal photos – if you do post pictures, make sure to adjust your privacy settings so strangers are unable to view your profile.

Don’t post drunken photos of yourself on the Internet! It may be funny in the context of your friends but any content you post to the Internet is indexed, cached and eventually findable. Future employers (maybe future voters and journalists will search your information online; when they do, what do you want them to see?

Technorati Improves Blog Crawling Engine

Saturday, February 7th, 2009

Anyone who has done their homework in regards to blogging knows that Technorati is a major player in that space. At one point, one could not have considered their blog truly launched until they were present in the service’s database – being part of that directory meant you had made it. Now there are so many sites vying for attention that simply adding oneself isn’t enough, but it is still an important step in an overall marketing plan.

One victim of Technorati’s success has been its blog crawler – that piece of software that scans its directories looking for posts and updates. As time marches on, new developments and ways of interacting spring up which ultimately make cataloging the blogosphere more complex. Many site owners were complaining that it took too long for Technorati to update their listings – they had to manually ping the service every time they posted new content.

Fortunately they have announced an upgrade to their crawler (they say it’s a total rewrite from the ground up) that promises to improve the speed and accuracy of Technorati’s blogosphere scans – what and who are blogs talking about over time.

This is great news in general – Technorati has been a great source of traffic and useful information; with the continued improvements we hope to see it continue to be a big part of our blogging experience.

atoi in Flex/ActionScript – Converting Strings to Numbers

Saturday, February 7th, 2009

It couldn’t be easier to convert a string to a number in Flex Actionscript. The language doesn’t have (or need) an atoi() function as seen in languages like C – it’s as simple as creating a cast:

[source:JavaScript]
var myString:String = “15”;
var myNum:Number = Number( myString );
[/source]

The framework will convert any data type, not just strings. If it is unable to do so, it will return 0 (for ints) or NaN (for floating point).

Six Flavours of Windows 7

Thursday, February 5th, 2009

I’m starting to get enthused about Windows 7. Not so excited that I would stand in a line overnight to be the first to own a copy, mind, but I may actually be putting a PC back in the running for my next computer. One thing I am not so thrilled about is the six different versions to choose between.

Come on, guys. Mac has it right – one version of the OS for desktop use, another for server configurations. Do you really need a separate version for:

  1. Netbooks (essentially, a stripped-down driver-free version that can fit on solid state flash drives)
  2. Home “basic” Version (for emerging markets where piracy is a problem – do people seriously pay for these crippled versions when they can steal the full thing for nothing?)
  3. Home Premium (by all accounts nothing is “premium” about this version, it’s just the standard consumer-level version)
  4. Professional (meaning what, exactly?)
  5. Enterprise (for offices – the difference between this any professional has to do with site licensing)
  6. Ultimate (includes all the above)

I charge that the “Ultimate” version should be the “Only” version. Microsoft should quit playing games and obfuscating its software licenses – just sell the product; don’t make customers research all of the possible variations on features and functionality.