UML:
Sample:
Prototype()
- Declares an interface for cloning itself
- Define the interface for copying objects
Concreteprototype()
- Implements an operation for cloning itself
- Copy your own operations
Client()
- Creates a new object by asking a prototype to clone itself
- Create a new object to copy by accessing prototype
Prototype:
1 Abstract Class Prototype
2 {
3 Private String _ Id;
4 Public Prototype ( String ID)
5 {
6 This . _ Id = ID;
7 }
8 Public String ID
9 {
10 Get { Return _ Id ;}
11 }
12 Public Abstract Prototype clone ();
13 }
Concreteprototype1:
1 Class Concreteprototype1: Prototype
2 {
3 Public Concreteprototype1 ( String ID)
4 : Base (ID)
5 {}
6
7 Public Override Prototype clone ()
8 {
9 Return (Prototype) This . Memberwiseclone ();
10 }
11 }
Concreteprototype2:
1 Class Concreteprototype2
2 {
3 Public Concreteprototype2 ( String ID)
4 : Base (ID)
5 {}
6 Public Override Prototype clone ()
7 {
8 Return (Prototype) This . Memberwiseclone ();
9 }
10 }
Program:
1 # Region Prototype
2 Concreteprototype1 p1 = New Concreteprototype1 ( " I " );
3 Concreteprototype1 C1 = (Concreteprototype1) p1.clone ();
4 Console. writeline ( " Cloned: {0} " , C1.id );
5 Concreteprototype2 p2 = New Concreteprototype2 ( " II " );
6 Concreteprototype2 C2 = (Concreteprototype2) p2.clone ();
7 Console. writeline ( " Cloned: {0} " , C2.id );
8 Console. readkey ();
9 # Endregion
The prototype is to request a prototype to clone the object itself.