Home > Development > Prototype Pattern

Prototype Pattern

September 12, 2010 Leave a comment Go to comments

Definition

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

UML

The code is pretty simple. Nice clean interface that supports cloning.

using System;

namespace PrototypePattern
{

    public interface ICloneable<T>
    {
        T Clone();
    }

    public class Customer : ICloneable<Customer>
    {
        public string Name { get; set; }

        public Customer(string name)
        {
            Name = name;
        }

        public Customer Clone()
        {
            string clonedname = String.IsNullOrEmpty(Name) ? String.Empty : String.Copy(Name);
            return new Customer(clonedname);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Customer c = new Customer("Blair");
            Customer d = c.Clone();

            Console.WriteLine(d.Name);

            Console.ReadKey();
        }
    }
}

Blair…

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: