📊 Comparison
| Aspect | Without Prototype (new) | With Prototype (clone) |
|---|---|---|
| Creation | Build from scratch | Copy existing |
| Speed | ❌ Slow | ✅ Fast |
| CPU | ❌ High | ✅ Low |
| Memory | ✅ Same | ✅ Same |
| Use case | Simple objects | Complex/expensive objects |
| Risk | ✅ Safe | ⚠️ Copy issues |
🔁 Shallow vs Deep Copy
| Type | Description | Speed | Risk |
|---|---|---|---|
| Shallow | Copies references | ✅ Fast | ❌ Shared data |
| Deep | Full independent copy | ❌ Slower | ✅ Safe |
var r1 = new Report();
var r2 = new Report(); // expensive again
using System;
class Report : ICloneable
{
public string Data;
public Report()
{
Console.WriteLine("Expensive creation...");
}
public object Clone()
{
return this.MemberwiseClone();
}
}
// Usage
var original = new Report();
var copy1 = (Report)original.Clone();
var copy2 = (Report)original.Clone();
public object Clone()
{
return new Report
{
Data = this.Data
};
}
No comments:
Post a Comment