<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Always Get Better &#187; Design Patterns</title>
	<atom:link href="http://www.alwaysgetbetter.com/blog/category/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alwaysgetbetter.com/blog</link>
	<description>Never stop looking for ways to improve</description>
	<lastBuildDate>Wed, 02 Jun 2010 12:16:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Implementing Lazy Load Using a Proxy Class</title>
		<link>http://www.alwaysgetbetter.com/blog/2009/02/28/implementing-lazy-load-proxy-class/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2009/02/28/implementing-lazy-load-proxy-class/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 20:28:56 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[efficiency]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=183</guid>
		<description><![CDATA[
 photo credit: Gog Llundain
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 &#8211; The object is [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div class="alignright"><a title="Relaxing in Slippers" href="http://www.flickr.com/photos/76045572@N00/3258171732/" target="_blank"><img src="http://farm4.static.flickr.com/3096/3258171732_38e877e923_m.jpg" border="0" alt="Relaxing in Slippers" /></a><br />
<small><a title="Attribution-ShareAlike License" href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank"><img src="http://www.alwaysgetbetter.com/blog/wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="Gog Llundain" href="http://www.flickr.com/photos/76045572@N00/3258171732/" target="_blank">Gog Llundain</a></small></div>
<p>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.</p>
<p>There are typically three ways of implementing lazy load:<br />
1. Lazy Initialization &#8211; The object is set to <em>null</em> and checked/loaded when data is needed<br />
2. Proxy &#8211; 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<br />
3. Ghost &#8211; The class loads only a partial set of its information until more is needed</p>
<h2>Example Situation</h2>
<p>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.</p>
<div id="attachment_184" class="wp-caption aligncenter" style="width: 152px"><img class="size-full wp-image-184" title="Artist Class" src="http://www.alwaysgetbetter.com/blog/wp-content/uploads/2009/02/artist_class.png" alt="UML for Artist Class" width="142" height="155" /><p class="wp-caption-text">UML for Artist Class</p></div>
<p>Let&#8217;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.</p>
<p>The solution is to create an ArtistProxy class. ArtistProxy has an _artist variable who is set to <em>null</em> when it is initialized. Whenever we try to access the artist&#8217;s name, web site, etc from ArtistProxy, the class creates an Artist (only if not already done) and returns the property from _artist.</p>
<p>Our print function is never aware of ArtistProxy &#8211; as far as it is concerned it only ever deals with Artist. We accomplish this by creating an interface &#8211; IArtist &#8211; 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.</p>
<div id="attachment_186" class="wp-caption aligncenter" style="width: 489px"><img class="size-full wp-image-186" title="Artist, ArtistProxy, and IArtist UML Diagram" src="http://www.alwaysgetbetter.com/blog/wp-content/uploads/2009/02/artist_lazyload.png" alt="Artist, ArtistProxy, and IArtist UML Diagram" width="479" height="368" /><p class="wp-caption-text">Artist, ArtistProxy, and IArtist UML Diagram</p></div>
<p>Now that we understand how our classes relate to each other, it&#8217;s time to use them in code:</p>
<p>// Our printArtists() function looks something like this.<br />
// Notice how we are unaware whether the artist is<br />
// an actual object, or a whether it is a proxy.<br />
public function printArtists( IArtist [] artistList )<br />
{<br />
  foreach ( IArtist artist in artistList )<br />
  {<br />
    printOneArtist( artist );<br />
  }<br />
}</p>
<p>// Implementation of the IArtist interface<br />
public interface IArtist<br />
{<br />
  string Name { get; set; }<br />
  string Genre { get; set; }<br />
  string Website { get; set; }<br />
  int getNumberOfAlbums();<br />
}</p>
<p>// Implementation of Artist class<br />
public class Artist : IArtist<br />
{<br />
  private int _id;<br />
  private string _name;<br />
  private string _genre;<br />
  private string _website;</p>
<p>  public Artist( id )<br />
  {<br />
    _id = id;<br />
  }</p>
<p>  public string Name<br />
  {<br />
    get { return _name; }<br />
    set { _name = value; }<br />
  }</p>
<p>  public string Genre<br />
  {<br />
    get { return _genre; }<br />
    set { _genre = value; }<br />
  }</p>
<p>  public string Website<br />
  {<br />
    get { return _website; }<br />
    set { _website = value; }<br />
  }</p>
<p>  public int getNumberOfAlbums()<br />
  {<br />
    return fictionalDataConnection-&gt;getNumberOfAlbums( _id );<br />
  }<br />
}</p>
<p>// Implementation of ArtistProxy class<br />
public class ArtistProxy : IArtist<br />
{<br />
  private Artist _artist;<br />
  private int _id;</p>
<p>  public ArtistProxy( id )<br />
  {<br />
    _artist = null;<br />
    _id = id;<br />
  }</p>
<p>  public string Name<br />
  {<br />
    get<br />
    {<br />
      if ( null == _artist ) _artist = new Artist( _id );<br />
      return _artist.Name;<br />
    }<br />
    set<br />
    {<br />
      if ( null == _artist ) _artist = new Artist( _id );<br />
      _artist.Name = value;<br />
    }<br />
  }</p>
<p>  public string Genre<br />
  {<br />
    get<br />
    {<br />
      if ( null == _artist ) _artist = new Artist( _id );<br />
      return _artist.Genre;<br />
    }<br />
    set<br />
    {<br />
      if ( null == _artist ) _artist = new Artist( _id );<br />
      _artist.Genre = value;<br />
    }<br />
  }</p>
<p>  public string Website<br />
  {<br />
    get<br />
    {<br />
      if ( null == _artist ) _artist = new Artist( _id );<br />
      return _artist.Website;<br />
    }<br />
    set<br />
    {<br />
      if ( null == _artist ) _artist = new Artist( _id );<br />
      _artist.Website = value;<br />
    }<br />
  }</p>
<p>  public int getNumberOfAlbums()<br />
  {<br />
    if ( null == _artist ) _artist = new Artist( _id );<br />
    return _artist.getNumberOfAlbums();<br />
  }<br />
}</p>
<p>Of course for the sake of convenience a few things are missing from my example:<br />
1. An actual data source<br />
2. Delegates (presumably one would include a &#8216;LoadArtist&#8217; delegate so the proxy will be able to pass the actual loading of its artist to the data layer)</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2009/02/28/implementing-lazy-load-proxy-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
