Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

An example of Prototype pattern

0.00/5 (No votes)
7 Feb 2011CPOL 12.6K  
An example of Prototype pattern
The Prototype pattern creates new objects by cloning one of a few stored prototypes.

I desided to apply this pattern for a small application to sell different devices.

C#
internal abstract class DevicePrototype
    {
        public abstract DevicePrototype Clone();
    }


This class defines the interface that says prototypes must be cloneable. Now we need to create a class with cloning capabilities. In our situation it'll be class represented some device:

C#
internal class Device: DevicePrototype
    {
        public Guid Guid { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public string Owner { get; set; }
        public override DevicePrototype Clone()
        {
            Console.WriteLine("Cloning device - Name: {0}, Price: {1}",
                Name, Price);
            return MemberwiseClone() as DevicePrototype;
        }
        public override string ToString()
        {
            return string.Format("Guid:{0}\nName:{1}\nPrice{2}\nOwner{3}\n\n",
                Guid, Name, Price, Owner);
        }
    }


Then we'll create prototype maneger. In our case we'll use this manager as shop to store devices for selling.

internal class DeviceManager
    {
        private Hashtable devices=new Hashtable();

        public DevicePrototype this[string name]
        {
            get
            {
                return devices.ContainsKey(name)
                           ? devices[name] as DevicePrototype
                           : null;
            }

            set
            {
                devices.Add(name, value);
            }
        }
    }


And now is the time to play with created classes to demonstrate how we use Prototype pattern:

C#
class Program
    {
        static void Main(string[] args)
        {
            //Creates A SHOP / an instance of manager
            var shop = new DeviceManager();
            //Creates a list to store sold devices
            var devices = new List<Device>();
            //Fills product list in the shop with products
            shop["iPhone"] = new Device {Guid = Guid.Empty, 
          Name = "iPhone", Price = 1000};
            shop["iPad"] = new Device {Guid = Guid.Empty, 
          Name = "iPad", Price = 1200};
            //Simulates e-commerce process
            var iPhoneForDavid = shop["iPhone"].Clone() as Device;
            iPhoneForDavid.Guid = Guid.NewGuid();
            iPhoneForDavid.Owner = "David";
            devices.Add(iPhoneForDavid);
            var iPhoneForSara = shop["iPhone"].Clone() as Device;
            iPhoneForSara.Guid = Guid.NewGuid();
            iPhoneForSara.Owner = "Sara";
            devices.Add(iPhoneForSara);
            var iPodForSara = shop["iPad"].Clone() as Device;
            iPodForSara.Guid = Guid.NewGuid();
            iPodForSara.Owner = "Sara";
            devices.Add(iPodForSara);
            //Let's show some results
            Console.WriteLine("\n\n\nShow all iPhones");
            Console.WriteLine("----------------------------");
            var iPhones = devices.Where(d => d.Name == "iPhone");
            foreach (var iPhone in iPhones)
            {
                Console.Write(iPhone);
            }
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)