您的位置:首页 > 其它

设计模式 原型模式(Prototype Pattern)

2012-03-28 15:29 204 查看
介绍

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
Prototype原型模式是一种创建型设计模式,Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。
解决什么问题:它主要面对的问题是:“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是他们却拥有比较稳定一致的接口。

原型模式强调的是用已有对象克隆出新对象,是对象的创建便方便,不需要重复的new操作。原型模式的原型中一般会有一个Clone方法,其子类实现该方 法,用于复制出新的对象,在此之前,首先要初始化一大堆可能会出现的对象。前面提到的Clone方法的实现有两种方法,分别是浅拷贝和 深拷贝,我这里实现为浅拷贝。

有个学员信息实体对象、现在我们需要浅拷贝和 深拷贝。类结构如下图



MsgModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class MsgModel
{
/// <summary>
///  构造函数
/// </summary>
/// <param name="name">人员信息名称</param>
/// <param name="age">人员信息年龄</param>
/// <param name="birthday">人员信息生日</param>
public MsgModel(string name, int age, DateTime birthday)
{
this._age = age;
this._name = name;
this._birthday = birthday;
}
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;

public int Age
{
get { return _age; }
set { _age = value; }
}
private DateTime _birthday;

public DateTime Birthday
{
get { return _birthday; }
set { _birthday = value; }
}
}
}


浅拷贝(实现拷贝操作,在.NET中可以使用Object类的MemberwiseClone()方法来实现对象的浅表拷贝)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
/// <remarks>ddd</remarks>
public class ShallowCopy : ICloneable
{
/// <summary>
/// 构造函数
/// </summary>
public ShallowCopy()
{

}

/// <summary>
///     实现ICloneable的Clone()方法
/// </summary>
/// <returns></returns>
public object Clone()
{
return this.MemberwiseClone();
}
private MsgModel mm;
/// <summary>
///人员信息 实体对象
/// </summary>
public MsgModel MsgModel
{
get { return mm; }
set { mm = value; }
}
}
}


深拷贝(这里的深度拷贝采用了重新返回一个对象的方式,实现深度拷贝的方式很多)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class DeepCopy : ICloneable
{
/// <summary>
/// 构造函数
/// </summary>
public DeepCopy()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="mm">人员信息实体对象</param>
public DeepCopy(MsgModel mm)
{
this.mm = mm;
}
/// <summary>
/// 实现ICloneable的Clone()方法
/// </summary>
/// <returns></returns>
public object Clone()
{
return new DeepCopy(new MsgModel(mm.Name, mm.Age, mm.Birthday));
}

private MsgModel mm;
/// <summary>
///人员信息 实体对象
/// </summary>
public MsgModel MsgModel
{
get { return mm; }
set { mm = value; }
}

}
}


主函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***************浅拷贝************");

ShallowCopy shallow1 = new ShallowCopy();
ShallowCopy shallow2 = new ShallowCopy();
shallow1.MsgModel = new MsgModel("Tom", 18, DateTime.Now);
shallow2 = (ShallowCopy)shallow1.Clone();

Console.WriteLine("shallow1--->>name:{0},age:{1},birthday:{2}", shallow1.MsgModel.Name, shallow1.MsgModel.Age, shallow1.MsgModel.Birthday);
Console.WriteLine("shallow2--->>name:{0},age:{1},birthday:{2}", shallow2.MsgModel.Name, shallow2.MsgModel.Age, shallow2.MsgModel.Birthday);
Console.WriteLine("修改shallow1中的值");
shallow1.MsgModel.Name = "jarry";
shallow1.MsgModel.Age = 20;
shallow1.MsgModel.Birthday = DateTime.Now.AddDays(1);
Console.WriteLine("shallow1--->>name:{0},age:{1},birthday:{2}", shallow1.MsgModel.Name, shallow1.MsgModel.Age, shallow1.MsgModel.Birthday);
Console.WriteLine("shallow2--->>name:{0},age:{1},birthday:{2}", shallow2.MsgModel.Name, shallow2.MsgModel.Age, shallow2.MsgModel.Birthday);
Console.WriteLine("***************浅拷贝************");

Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");

Console.WriteLine("***************深拷贝************");
DeepCopy deep1 = new DeepCopy();
DeepCopy deep2 = new DeepCopy();
deep1.MsgModel = new MsgModel("Tom", 18, DateTime.Now);
deep2 = (DeepCopy)deep1.Clone();
Console.WriteLine("deep1--->>name:{0},age:{1},birthday:{2}", deep1.MsgModel.Name, deep1.MsgModel.Age, deep1.MsgModel.Birthday);
Console.WriteLine("deep2--->>name:{0},age:{1},birthday:{2}", deep2.MsgModel.Name, deep2.MsgModel.Age, deep2.MsgModel.Birthday);
Console.WriteLine("修改deep1中的值");
deep1.MsgModel.Name = "jarry";
deep1.MsgModel.Age = 20;
deep1.MsgModel.Birthday = DateTime.Now.AddDays(1);
Console.WriteLine("deep1--->>name:{0},age:{1},birthday:{2}", deep1.MsgModel.Name, deep1.MsgModel.Age, deep1.MsgModel.Birthday);
Console.WriteLine("deep2--->>name:{0},age:{1},birthday:{2}", deep2.MsgModel.Name, deep2.MsgModel.Age, deep2.MsgModel.Birthday);
Console.WriteLine("***************深拷贝************");

}
}
}


运行效果图





创建模型小结

工厂模式(Factory Pattern)根据提供给工厂的数据,从一系列相关的类中选择一个类实例并返回。
抽象工厂模式(Abstract Factory Pattern)用于返回一组类中的一个,在某些情况下,它实际上为了一组类返回了一个工厂。
生成器模式(Builder Pattern)根据提供给它的数据及其表示,将一系列对象组装成一个新的对象。通常选择何种方式组装对象有工厂决定。
单件模式(singleton Pattern)可以保证有且有一个对象实例,并提供一个该类的全局访问点。
原型模式(Prototype Pattern)用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: