Prototype Pattern

In this article I would like to explain the Prototype Pattern and C# support for it. The prototype pattern provides an alternative to instantiating new objects by copying the prototype of an existing one.

Challenge

You are working on an application having setting stored in class Settings. The properties of the class are assignedinside the constructor. It involves calling the configuration file, security database, user profile database etc.

You need to pass the above instance to a component inside a library. The component could play around with the properties so we need to send only another instance of Settings. As the instance creation is expensive we need to create a copy of the above instance.

What would be the best approach?

Definition

“Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype”

Solution

We can use the Prototype pattern to address this scenario. This pattern allows us to create a new instance by prototyping an existing instance with required properties.

clip_image002

The System.object class includes new method named MemberwiseClone() which provides a shallow copy of the current object. Shallow copy involves copying only the value types. Coupled with ICloneable interface we can provide a Clone() method that invokes MemberwiseClone() for the Settings class.

publicclassSettings : ICloneable

{

public Settings()

{

// Load ApplicationSettings from Configuration

// Load ThemeSettings from Database

// Load UserSettings from Database

Thread.Sleep(3000); // Simulate a delay for the above operations

ApplicationSettings = “ApplicationSettings1”;

ThemeSettings = “ThemeSettings1”;

UserSettings = “UserSettings1”;

}

publicstringApplicationSettings

{

get;

set;

}

publicstringThemeSettings

{

get;

set;

}

publicstringUserSettings

{

get;

set;

}

publicobject Clone()

{

returnthis.MemberwiseClone();

}

publicoverridestringToString()

{

returnthis.ApplicationSettings + ” ” +

this.ThemeSettings + ” ” +

this.UserSettings;

}

}

Executing the Application

Now we can try playing with the class instances. First we create an instance of Settings (which is a 3 second delay process). Later we take a clone of the Settings instance and modify the properties. We can see that the new instance was created using Clone() method.

staticvoid Main(string[] args)

{

Settingssettings = newSettings();

SettingssettingsClone = (Settings)settings.Clone();

settingsClone.ApplicationSettings = “NewSettings”; // Change property

Console.WriteLine(“INSTANCE 1: ” + settings.ToString());

Console.WriteLine(“INSTANCE 2: ” + settingsClone.ToString());

Console.ReadKey(false);

}

On executing the application we can see the following results.

clip_image004

We can see from the above code that:

1. Instance 1 was created using new keyword

2. Instance 2 was created using Clone() method

3. Changing of Instance 2 does not affect Instance 1

So this concludes our experiment with the Prototype pattern.

References

http://sourcemaking.com/design_patterns/prototype

Summary

In this article we have explored the Prototype pattern. The usage of Prototype pattern can be summarized as:

· Faster instance creation with required properties

· The new keyword can be avoided in instance creation

· Provides an alternative to Abstract Factory pattern

· Runtime specification of instance properties

· Reference of concrete class can be avoided

The associated source code contains the example we discussed.

2 thoughts on “Prototype Pattern

  1. NILESH GULE says:

    Nice post. I recently posted about Prototype Design Pattern using Store creator as an example. Here is a link to my post http://www.nileshgule.com/2012/06/prototype-design-pattern.html

    1. Jean Paul says:

      Hei Nilesh!

      Good one and of high quality.

Leave a Reply to Jean Paul Cancel reply

Your email address will not be published. Required fields are marked *