Prototype Design Pattern: This pattern is used when objects or instances are created by cloning other object or instances. It allows to add any sub class instance of known super class instance. It is used when there are numerous potential classes , that are ready to use when needed.
It falls under creational design pattern.
Example: C0de is also available at GitHub. Click here to go to git hub repository.
namespace PrototypeDesignPattern
{
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
Cat cloneCat = (Cat)cat.CLone();
Dog dog = new Dog();
Dog cloneDog = (Dog)dog.CLone();
Console.ReadLine();
}
}
public interface Animal
{
Animal CLone();
}
public class Cat : Animal
{
public Animal CLone()
{
Cat cat = null;
cat = (Cat) this.MemberwiseClone();
return cat;
}
}
public class Dog:Animal
{
public Animal CLone()
{
Dog dog = null;
dog = (Dog)this.MemberwiseClone();
return dog;
}
}
}