Home > Patterns and Practices > Prototype Pattern

Prototype Pattern

Prototype Pattern

Problem:

Most of the state of objects is going to be same barring a few of the state which is customizable.  In otherwise you create an object from the same template and customize it according to your needs. In other words Insurance agents work with the same standard policies and customize it slightly to suit the individual needs. How do we address this?

 

Solution:

The GoF class structure diagram for a prototype is as follows:

Assume you need to create a MarketingVehicle (Marketing vehicles is a term in Distribution domain, meaning medium of marketing – could be something like eMail, Print, Web Sites etc.) The start date end date etc could vary based on the fiscal year.

 

public class PolicyFactory

{

            private Policy standardPolicy;

 

            public PolicyFactory(Policy standardPolicy)

            {

                        this.standardPolicy =  standardPolicy;

            }

 

            public void changeStandardPolicy(Policy standardPolicy)

            {

                        this.standardPolicy =  standardPolicy;

}

 

public Policy CreatePolicy()

{

            standardPolicy.Clone();

}

}

 

public abstract class Policy

{

            protected Profile holders;

            protected PolicyCoverages coverages;

            protected PolicyRiders riders;

            protected InstallementSchedule schedule;           

protected ProfileCollection insureds;       

 

            abstract public Policy Clone();

           

}

 

public class HomePolicy : Policy

{

            // Home policy specific attributes Like Home address, items covered etc

           

            public Policy Clone()

            {

                        return new HomePolicy();

}

}

 

public class AutomobilePolicy : Policy

{

            // Automobile policy specific attributes like Vehicle Number, Vehicle Type etc.

           

            public Policy Clone()

            {

                        return new AutomobilePolicy();

}

}

 

public class Program

{

            public static void Main()

            {

                        Policy homePolicy = new HomePolicy();

                       

                        // Set attributes which are common for most of the policies 

// This will be our template object

 

PolicyFactory factory = new PolicyFactory(homePolicy);

 

Policy policy1, policy2, policy3;

policy1 = factory.CreatePolicy();

// customize it

 

policy2 = factory.CreatePolicy();

// customize it

 

policy1 = factory.CreatePolicy();

// customize it

            }

}

 

Conceptual class diagram of the above example:

 

Advertisements
  1. varghese
    January 18, 2006 at 6:52 am

    Dear voyagers of cyber space, I am doing my thesis on Mobile Games in India and I would appreciate it if you could please click on the link below and fill this questionnaire. You don’t have to be a gamer, just have to own or use a mobile phone on a regular basis. I would also request you to forward this mail to your friends asking them to fill this questionnaire. I assure you that filling it wont take more than a few minutes of your time. http://www.my3q.com/home2/87/vkc/7586.phtml The questionnaire is self explanatory. Please submit it after you are done with it.

  1. November 19, 2010 at 11:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: