Prototype — RexDac Academy

RexDac AcademyTechnical notes · Some prior knowledge helps
0%Published notes read · this device

Principles and Architecture / Design patterns / Creational

Prototype

Original notes by SunDiaL · View English source

The problem: You need to duplicate an object. The additional problem is that in order to do that you have to know how, which implies knowing the type of the instance (inheritance) and having access to the classes private fields. The solution: Information Expert. Who is the most suited class to solve these problems? The class itself.

public class PrototypeExample
{
	private string somePrivateField;
	
	public PrototypeExample Clone()
	{
		return new PrototypeExample
		{
			somePrivateField = this.somePrivateField;
		}
	}
}

.Net has an ICloneable interface for this purpose.