Design Mode: Prototype
1. Class Diagram
Instance class diagram
2. Create a project
..........................................
3. Create a weekly report WeeklyLog: act as the prototype. The Clone () method is used to Clone the prototype object. Attachmentch acts as the member class.
The Attachmentch code is as follows:
Using System;
Namespace PrototypeSample
{
[Serializable]
Class Attachment
{
Private string name;
Public string Name
{
Get {return name ;}
Set {name = value ;}
}
Public void Download ()
{
Console. WriteLine ("Download Attachment, file name: {0 }. ", Name );
}
}
}
4. The WeeklyLog class code is as follows:
Using System;
Using System. IO;
Using System. Collections;
Using System. Runtime. Serialization. Formatters. Binary;
Using System. Runtime. Serialization;
Namespace PrototypeSample
{
[Serializable]
Class WeeklyLog
{
Private Attachment attachment;
Private string name;
Private string date;
Private string content;
Public Attachment
{
Get {return attachment ;}
Set {attachment = value ;}
}
Public string Name
{
Get {return name ;}
Set {name = value ;}
}
Public string Date
{
Get {return date ;}
Set {date = value ;}
}
Public string Content
{
Get {return content ;}
Set {content = value ;}
}
/*
// Use the MemberwiseClone () method for shortest cloning
Public WeeklyLog Clone ()
{
Return this. MemberwiseClone () as WeeklyLog;
}
*/
// Achieve deep cloning using serialization
Public WeeklyLog Clone ()
{
WeeklyLog clone = null;
// Serialization
FileStream fs = new FileStream ("Temp. dat", FileMode. Create );
BinaryFormatter formatter = new BinaryFormatter ();
Try
{
// Serialize the current object to Temp. dat
Formatter. Serialize (fs, this );
}
Catch (SerializationException e)
{
Console. WriteLine ("Failed to serialize. Reason:" + e. Message );
Throw;
}
Finally
{
Fs. Close ();
}
// Deserialization
FileStream fs1 = new FileStream ("Temp. dat", FileMode. Open );
BinaryFormatter formatter1 = new BinaryFormatter ();
Try
{
// Deserialize from stream to object
Clone = (WeeklyLog) formatter1.Deserialize (fs1 );
}
Catch (SerializationException e)
{
Console. WriteLine ("Failed to deserialize. Reason:" + e. Message );
Throw;
}
Finally
{
Fs. Close ();
}
Return clone;
}
}
}
5. Client test class Program:
Using System;
Namespace PrototypeSample
{
Class Program
{
Static void Main (string [] args)
{
/*
ConcretePrototypeB prototype, copy;
Prototype = new ConcretePrototypeB ();
// Prototype. Attr = "Sunny ";
Copy = (ConcretePrototypeB) prototype. Clone ();
// Copy. Attr = "Tom ";
Console. WriteLine (prototype = copy );
// Console. WriteLine (prototype. GetType () = copy. GetType ());
Console. WriteLine (prototype. Member = copy. Member );
Console. Read ();
*/
WeeklyLog log_previous, log_new;
Log_previous = new WeeklyLog ();
Attachment attachment = new Attachment ();
Log_previus.attachment = attachment;
Log_new = log_previus.clone ();
Console. WriteLine ("is the weekly report the same? {0} ", (log_previous = log_new )? "Yes": "no ");
Console. WriteLine ("is the attachment the same? {0} ", (log_previus.attachment = log_new.Attachment )? "Yes": "no ");
Console. Read ();
}
}
}
6. Compile and run the program and output the following results:
7. Deep clone solution:
1) Mark weekly WeeklyLog and Attachment class Attachment as Serializable)
[Serializable]
Class WeeklyLog
{
Private Attachment attachment;
......
}
[Serializable]
Class Attachment
{
......
}
// Achieve deep cloning using serialization
Public WeeklyLog Clone ()
{
WeeklyLog clone = null;
FileStream fs = new FileStream ("Temp. dat", FileMode. Create );
BinaryFormatter formatter = new BinaryFormatter ();
Try
{
Formatter. Serialize (fs, this); // serialization
}
Catch (SerializationException e)
{
Console. WriteLine ("Failed to serialize. Reason:" + e. Message );
Throw;
}
Finally
{
Fs. Close ();
}
FileStream fs1 = new FileStream ("Temp. dat", FileMode. Open );
BinaryFormatter formatter1 = new BinaryFormatter ();
Try
{
Clone = (WeeklyLog) formatter1.Deserialize (fs1); // deserialization
}
Catch (SerializationException e)
{
Console. WriteLine ("Failed to deserialize. Reason:" + e. Message );
Throw;
}
Finally
{
Fs1.Close ();
}
Return clone;
}