Virtual Proxy Pattern
August 19, 2010 1 Comment
As I said in an early post this is the first (of four) posts about the proxy design pattern, in this case I’m going to cover the virtual proxy.
Virtual Proxies
This kind of proxies are related to lazy loading, let’s suppose we have a photo album, which has photos – of course – in it, so we want to get the album without loading every picture, because if the album has thousands of pictures, we will filling up the memory with data we don’t need yet, so this is when we could implement a virtual proxy pattern.
In the following example, I have created a virtual proxy pattern which will load the pictures if needed when needed. The example goes like this.
Step 1: Create an interface for the photo object that will load the picture when needed (IPhoto.cs)
using System;
namespace Structural.Proxy.Virtual.Base
{
public interface IPhoto
{
void Show();
}
}
Step 2: We will need our real object, in our case, the photo object (Photo.cs)
using System;
using Structural.Proxy.Virtual.Base;
namespace Structural.Proxy.Virtual.Picture
{
public class Photo : IPhoto
{
public string Name { get; private set; }
public Photo(string name)
{
Name = name;
Load();
}
public void Show()
{
Console.WriteLine("----> Displaying photo '{0}')", Name);
}
private void Load()
{
Console.WriteLine("------> Loading photo '{0}')", Name);
}
}
}
Step 3: Now we will create the proxy object (PhotoProxy.cs)
using System;
using Structural.Proxy.Virtual.Base;
namespace Structural.Proxy.Virtual.Picture
{
public class PhotoProxy : IPhoto
{
IPhoto photo;
public string Name { get; private set; }
public PhotoProxy(string name)
{
Name = name;
}
public void Show()
{
Console.WriteLine("----> Displaying photo '{0}')", Name);
if (photo == null)
{
photo = new Photo(Name);
}
}
}
}
Step 4: Create our photo album (PhotoAlbum.cs) (How did you guess?)
using System;
using System.Collections.Generic;
using Structural.Proxy.Virtual.Base;
namespace Structural.Proxy.Virtual.Picture
{
public class PhotoAlbum
{
List<IPhoto> photos;
public string Name { get; private set; }
public PhotoAlbum(string name)
{
Name = name;
photos = new List<IPhoto>();
}
public void AddPhoto(IPhoto photo)
{
photos.Add(photo);
}
public void Slideshow()
{
Console.WriteLine("--> Starting '{0}' slideshow)", Name);
foreach (IPhoto iPhoto in photos)
{
iPhoto.Show();
}
Console.WriteLine("--> Slideshow for '{0}' is finished)\n", Name);
}
}
}
Step 5: Now let’s create our fake data provider (DataProvider.cs)
using System;
using Structural.Proxy.Virtual.Picture;
namespace Structural.Proxy.Virtual.FakeProvider
{
public static class DataProvider
{
public static PhotoAlbum GetData()
{
PhotoAlbum photoAlbum = new PhotoAlbum("Album 1");
photoAlbum.AddPhoto(new Photo("Photo 1"));
photoAlbum.AddPhoto(new Photo("Photo 2"));
photoAlbum.AddPhoto(new Photo("Photo 3"));
return photoAlbum;
}
public static PhotoAlbum GetProxyData()
{
PhotoAlbum photoAlbum = new PhotoAlbum("Album 2 (Proxy)");
photoAlbum.AddPhoto(new PhotoProxy("Proxy Photo 1"));
photoAlbum.AddPhoto(new PhotoProxy("Proxy Photo 2"));
photoAlbum.AddPhoto(new PhotoProxy("Proxy Photo 3"));
return photoAlbum;
}
}
}
Step 6: Finally we will implement our main method.
using System;
using System.Collections.Generic;
using Structural.Proxy.Virtual.FakeProvider;
using Structural.Proxy.Virtual.Picture;
namespace Structural.Proxy.Virtual
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("(Status: Step 1 - Getting data)\n");
PhotoAlbum photoAlbum = DataProvider.GetData();
PhotoAlbum photoAlbumProxy = DataProvider.GetProxyData();
Console.WriteLine("\n(Status: Step 2 - Starting slideshows)\n");
photoAlbum.Slideshow();
photoAlbumProxy.Slideshow();
Console.WriteLine("(Status: Finished)");
Console.ReadKey();
}
}
}
Step 7: Press f5, and if you have time, sit back and watch the magic of the virtual proxy pattern.
(Status: Step 1 - Getting data) ------> Loading photo 'Photo 1') ------> Loading photo 'Photo 2') ------> Loading photo 'Photo 3') (Status: Step 2 - Starting slideshows) --> Starting 'Album 1' slideshow) ----> Displaying photo 'Photo 1') ----> Displaying photo 'Photo 2') ----> Displaying photo 'Photo 3') --> Slideshow for 'Album 1' is finished) --> Starting 'Album 2 (Proxy)' slideshow) ----> Displaying photo 'Proxy Photo 1') ------> Loading photo 'Proxy Photo 1') ----> Displaying photo 'Proxy Photo 2') ------> Loading photo 'Proxy Photo 2') ----> Displaying photo 'Proxy Photo 3') ------> Loading photo 'Proxy Photo 3') --> Slideshow for 'Album 2 (Proxy)' is finished) (Status: Finished)
Well that’s all as for the virtual proxy pattern, I really hope that this could help.
Rates and/or comments will be greatly appreciated ;)
Regards,
Esteban.
The Official Microsoft ASP.NET Site
Pingback: Proxy Pattern « A blog by Esteban Murchio