设计模式之原型模式
2018-06-22 07:40:59来源:未知 阅读 ()
1、类图
实例类图
2、创建项目
……………………………………
3、 新建周报类WeeklyLog:充当原型角色,Clone()方法为克隆方法,用于实现原型对象的克隆,Attachmentch充当成员类。
Attachmentch代码如下:
using System;
namespace PrototypeSample
{
[Serializable]
class Attachment
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public void Download()
{
Console.WriteLine("下载附件,文件名为{0}。",name);
}
}
}
4、WeeklyLog类代码如下:
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 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; }
}
/*
//使用MemberwiseClone()方法实现浅克隆
public WeeklyLog Clone()
{
return this.MemberwiseClone() as WeeklyLog;
}
*/
//使用序列化方式实现深克隆
public WeeklyLog Clone()
{
WeeklyLog clone = null;
//序列化
FileStream fs = new FileStream("Temp.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try
{
//对当前对象执行序列化操作到Temp.dat
formatter.Serialize(fs, this);
}
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);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
return clone;
}
}
}
5、 客户端测试类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_previous.Attachment = attachment;
log_new = log_previous.Clone();
Console.WriteLine("周报是否相同?{0}",(log_previous == log_new)?"是":"否");
Console.WriteLine("附件是否相同?{0}",(log_previous.Attachment == log_new.Attachment)?"是":"否");
Console.Read();
}
}
}
6、 编译及运行程序,输出如下结果:
7、 深克隆解决方案:
1) 将周报类WeeklyLog和附件类Attachment标记为可序列化(Serializable)
[Serializable]
class WeeklyLog
{
private Attachment attachment;
……
}
[Serializable]
class Attachment
{
……
}
//使用序列化方式实现深克隆
public WeeklyLog Clone()
{
WeeklyLog clone = null;
FileStream fs = new FileStream("Temp.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, this); //序列化
}
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); //反序列化
}
catch (SerializationException e)
{
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs1.Close();
}
return clone;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:AJAX基础
下一篇:ajax实现跨域请求
- .NET建造者模式讲解 2020-02-27
- 详解asp.net mvc CodeFirst模式数据库迁移步骤 2019-12-14
- 分享基于EF6、Unitwork、Autofac的Repository模式设计 2019-07-23
- RabbitMQ-Direct模式 2019-05-17
- asp.net—单例模式 2018-12-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash