Home
Manage Your Code
Snippet: Prototype Pattern (C#)
Title: Prototype Pattern Language: C#
Description: Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype. Views: 660
Author: Michael Gage Date Added: 3/1/2007
Copy Code  
1  abstract class Prototype
2  {
3    private string id;
4
5    // Constructor 
6    public Prototype(string id)
7    {
8      this.id = id;
9    }
10
11    // Property 
12    public string Id
13    {
14      get{ return id; }
15    }
16
17    public abstract Prototype Clone();
18  }
Usage
  // "ConcretePrototype1" 

  class ConcretePrototype1 : Prototype
  {
    // Constructor 
    public ConcretePrototype1(string id) : base(id) 
    {
    }

    public override Prototype Clone()
    {
      // Shallow copy 
      return (Prototype)this.MemberwiseClone();
    }
  }