Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#
Tip/Trick

An example of Prototype pattern

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Feb 2011CPOL 12.4K   4  
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)


Written By
Software Developer (Senior)
Belarus Belarus
Software developer with over 3 years of extensive experience in analysis, design and development. Recently, most interested in refactoring and design patterns applied to .NET Framework.

Core technologies I am using: OOP, OOD, DDD, TDD, N-tier applications, enterprise development.

Certificates:
Brainbench: .NET Framework 3.5 Fundamentals, Data Modeling Concepts, Web Design Concepts, C#

Microsoft: Exam 70-526: TS: Microsoft .NET Framework 2.0 - Windows-Based Client Development

INTUIT.RU: Development of Web-application ASP. NET Using Visual Studio. NET, Web applications in ASP.NET, Microsoft .NET Framework Distributed Applications Development.

Specialties
NET Framework : 2.0, 3.5, 4.0
Languages and technologies: C#, ASP.NET MVC 2, ASP.NET MVC 3, WCF, ASP.NET 4.0, Web Services, ADO.NET, LINQ, Entity Framework, NHibernate 3.0, JavaScript, HTML, CSS, XML, Ajax
RDBMS : SQL Server 2005, 2008, 2008 R2, MS Access.
Reporting: MS SQL Reporting Services, Crystal Reports

Comments and Discussions

 
-- There are no messages in this forum --