Design Mode: Prototype

Source: Internet
Author: User

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;

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.