Prototype Pattern | Creational design pattern
Prototype Pattern
The Prototype Pattern is a creational design pattern that deals with object creation by copying an existing object, known as the prototype. Instead of creating an object from scratch, you create new instances by copying an existing object, which serves as a prototype.
Key components and steps involved in the Prototype Pattern:
Prototype Interface/Abstract Class: Defines the methods for cloning itself. This interface or abstract class declares a method like clone().
Concrete Prototype: Implements the cloning method declared in the prototype interface. Objects of this class will be used as prototypes for cloning.
Client: Requests object creation by cloning the prototype. It interacts with the prototype to create new instances.
Advantages:
Reducing subclassing: It allows creating new objects by copying an existing one, eliminating the need for subclassing.
Dynamic object creation: Objects can be created dynamically at runtime based on client requirements.
Customization: The pattern allows creating variations of objects by modifying their cloned copies.
The Prototype Pattern promotes flexibility in object creation by using existing objects as prototypes, facilitating the creation of new instances through cloning.
________
using System;
// Step 1: Prototype Interfaceinterface ICloneableShape{ ICloneableShape Clone(); void Draw();}
// Step 2: Concrete Prototypeclass Circle : ICloneableShape{ private int radius;
public Circle(int radius) { this.radius = radius; }
public ICloneableShape Clone() { // Create a new Circle by copying the current instance return new Circle(this.radius); }
public void Draw() { Console.WriteLine($"Drawing a circle with radius {radius}"); }}
// Step 3: Clientclass Client{ static void Main() { // Creating a prototype circle Circle originalCircle = new Circle(5);
// Cloning the original circle to create a new one Circle clonedCircle = (Circle)originalCircle.Clone();
// Drawing both circles Console.WriteLine("Original Circle:"); originalCircle.Draw();
Console.WriteLine("Cloned Circle:"); clonedCircle.Draw(); }}
________
________
public class Configuration{ public string ApplicationName { get; set; } public int MaxConnections { get; set; } // Other configuration properties...}
________
________
public class ConfigurationManager{ private Configuration defaultConfig;
public ConfigurationManager(Configuration defaultConfig) { this.defaultConfig = defaultConfig; }
public Configuration CreateModifiedConfiguration(string appName, int maxConnections) { // Shallow copy the default configuration Configuration modifiedConfig = (Configuration)defaultConfig.Clone();
// Modify specific properties modifiedConfig.ApplicationName = appName; modifiedConfig.MaxConnections = maxConnections;
return modifiedConfig; }}
________
________
using System;using System.IO;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;
// Step 1: Prototype Interfaceinterface ICloneableShape{ ICloneableShape Clone(); void Draw();}
// Step 2: Concrete Prototype[Serializable]class Circle : ICloneableShape{ private int radius;
public Circle(int radius) { this.radius = radius; }
public ICloneableShape Clone() { // Deep copy using serialization using (MemoryStream memoryStream = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(memoryStream, this); memoryStream.Seek(0, SeekOrigin.Begin); return (ICloneableShape)formatter.Deserialize(memoryStream); } }
public void Draw() { Console.WriteLine($"Drawing a circle with radius {radius}"); }}
// Step 3: Clientclass Client{ static void Main() { // Creating a prototype circle Circle originalCircle = new Circle(5);
// Deep copying the original circle to create a new one Circle clonedCircle = (Circle)originalCircle.Clone();
// Drawing both circles Console.WriteLine("Original Circle:"); originalCircle.Draw();
Console.WriteLine("Cloned Circle:"); clonedCircle.Draw();
// Modifying the cloned circle's radius to demonstrate deep copy independence clonedCircle.Draw(); }}
________
________
using System;using System.Collections.Generic;using System.IO;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;
// Step 1: Prototype Interfaceinterface ICloneableDocument{ ICloneableDocument Clone(); void Display();}
// Step 2: Concrete Prototype[Serializable]class Document : ICloneableDocument{ public string Title { get; set; } public List<string> Sections { get; set; } public Style DocumentStyle { get; set; }
public Document(string title, List<string> sections, Style documentStyle) { Title = title; Sections = sections; DocumentStyle = documentStyle; }
public ICloneableDocument Clone() { // Deep copy using serialization using (MemoryStream memoryStream = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(memoryStream, this); memoryStream.Seek(0, SeekOrigin.Begin); return (ICloneableDocument)formatter.Deserialize(memoryStream); } }
public void Display() { Console.WriteLine($"Title: {Title}"); Console.WriteLine("Sections:"); foreach (var section in Sections) { Console.WriteLine($"- {section}"); } Console.WriteLine($"Document Style: {DocumentStyle}"); }}
// Style class (for simplicity)[Serializable]class Style{ public string Font { get; set; } public string Color { get; set; }
public Style(string font, string color) { Font = font; Color = color; }
public override string ToString() { return $"Font: {Font}, Color: {Color}"; }}
// Step 3: Clientclass Client{ static void Main() { // Creating a prototype document Document originalDocument = new Document("Sample Document", new List<string> { "Introduction", "Conclusion" }, new Style("Arial", "Black"));
// Deep copying the original document to create a new one Document clonedDocument = (Document)originalDocument.Clone();
// Modifying the cloned document to demonstrate deep copy independence clonedDocument.Title = "Cloned Document"; clonedDocument.Sections.Add("Appendix"); clonedDocument.DocumentStyle.Color = "Blue";
// Displaying both documents Console.WriteLine("Original Document:"); originalDocument.Display();
Console.WriteLine("\nCloned Document:"); clonedDocument.Display(); }}
________
Comments
Post a Comment