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

介绍

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

 

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

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

MsgModel

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 public class MsgModel
9 {
10 /// <summary>
11 /// 构造函数
12 /// </summary>
13 /// <param name="name">人员信息名称</param>
14 /// <param name="age">人员信息年龄</param>
15 /// <param name="birthday">人员信息生日</param>
16 public MsgModel(string name, int age, DateTime birthday)
17 {
18 this._age = age;
19 this._name = name;
20 this._birthday = birthday;
21 }
22 private string _name;
23
24 public string Name
25 {
26 get { return _name; }
27 set { _name = value; }
28 }
29 private int _age;
30
31 public int Age
32 {
33 get { return _age; }
34 set { _age = value; }
35 }
36 private DateTime _birthday;
37
38 public DateTime Birthday
39 {
40 get { return _birthday; }
41 set { _birthday = value; }
42 }
43 }
44 }

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

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 /// <remarks>ddd</remarks>
9 public class ShallowCopy : ICloneable
10 {
11 /// <summary>
12 /// 构造函数
13 /// </summary>
14 public ShallowCopy()
15 {
16
17 }
18
19 /// <summary>
20 /// 实现ICloneable的Clone()方法
21 /// </summary>
22 /// <returns></returns>
23 public object Clone()
24 {
25 return this.MemberwiseClone();
26 }
27 private MsgModel mm;
28 /// <summary>
29 ///人员信息 实体对象
30 /// </summary>
31 public MsgModel MsgModel
32 {
33 get { return mm; }
34 set { mm = value; }
35 }
36 }
37 }

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

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 public class DeepCopy : ICloneable
9 {
10 /// <summary>
11 /// 构造函数
12 /// </summary>
13 public DeepCopy()
14 {
15 }
16 /// <summary>
17 /// 构造函数
18 /// </summary>
19 /// <param name="mm">人员信息实体对象</param>
20 public DeepCopy(MsgModel mm)
21 {
22 this.mm = mm;
23 }
24 /// <summary>
25 /// 实现ICloneable的Clone()方法
26 /// </summary>
27 /// <returns></returns>
28 public object Clone()
29 {
30 return new DeepCopy(new MsgModel(mm.Name, mm.Age, mm.Birthday));
31 }
32
33 private MsgModel mm;
34 /// <summary>
35 ///人员信息 实体对象
36 /// </summary>
37 public MsgModel MsgModel
38 {
39 get { return mm; }
40 set { mm = value; }
41 }
42
43 }
44 }

主函数

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleApplication1
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Console.WriteLine("***************浅拷贝************");
13
14 ShallowCopy shallow1 = new ShallowCopy();
15 ShallowCopy shallow2 = new ShallowCopy();
16 shallow1.MsgModel = new MsgModel("Tom", 18, DateTime.Now);
17 shallow2 = (ShallowCopy)shallow1.Clone();
18
19 Console.WriteLine("shallow1--->>name:{0},age:{1},birthday:{2}", shallow1.MsgModel.Name, shallow1.MsgModel.Age, shallow1.MsgModel.Birthday);
20 Console.WriteLine("shallow2--->>name:{0},age:{1},birthday:{2}", shallow2.MsgModel.Name, shallow2.MsgModel.Age, shallow2.MsgModel.Birthday);
21 Console.WriteLine("修改shallow1中的值");
22 shallow1.MsgModel.Name = "jarry";
23 shallow1.MsgModel.Age = 20;
24 shallow1.MsgModel.Birthday = DateTime.Now.AddDays(1);
25 Console.WriteLine("shallow1--->>name:{0},age:{1},birthday:{2}", shallow1.MsgModel.Name, shallow1.MsgModel.Age, shallow1.MsgModel.Birthday);
26 Console.WriteLine("shallow2--->>name:{0},age:{1},birthday:{2}", shallow2.MsgModel.Name, shallow2.MsgModel.Age, shallow2.MsgModel.Birthday);
27 Console.WriteLine("***************浅拷贝************");
28
29
30 Console.WriteLine("");
31 Console.WriteLine("");
32 Console.WriteLine("");
33
34 Console.WriteLine("***************深拷贝************");
35 DeepCopy deep1 = new DeepCopy();
36 DeepCopy deep2 = new DeepCopy();
37 deep1.MsgModel = new MsgModel("Tom", 18, DateTime.Now);
38 deep2 = (DeepCopy)deep1.Clone();
39 Console.WriteLine("deep1--->>name:{0},age:{1},birthday:{2}", deep1.MsgModel.Name, deep1.MsgModel.Age, deep1.MsgModel.Birthday);
40 Console.WriteLine("deep2--->>name:{0},age:{1},birthday:{2}", deep2.MsgModel.Name, deep2.MsgModel.Age, deep2.MsgModel.Birthday);
41 Console.WriteLine("修改deep1中的值");
42 deep1.MsgModel.Name = "jarry";
43 deep1.MsgModel.Age = 20;
44 deep1.MsgModel.Birthday = DateTime.Now.AddDays(1);
45 Console.WriteLine("deep1--->>name:{0},age:{1},birthday:{2}", deep1.MsgModel.Name, deep1.MsgModel.Age, deep1.MsgModel.Birthday);
46 Console.WriteLine("deep2--->>name:{0},age:{1},birthday:{2}", deep2.MsgModel.Name, deep2.MsgModel.Age, deep2.MsgModel.Birthday);
47 Console.WriteLine("***************深拷贝************");
48
49
50 }
51 }
52 }

运行效果图



创建模型小结

工厂模式(Factory Pattern)根据提供给工厂的数据,从一系列相关的类中选择一个类实例并返回。
抽象工厂模式(Abstract Factory Pattern)用于返回一组类中的一个,在某些情况下,它实际上为了一组类返回了一个工厂。
生成器模式(Builder Pattern)根据提供给它的数据及其表示,将一系列对象组装成一个新的对象。通常选择何种方式组装对象有工厂决定。
单件模式(singleton Pattern)可以保证有且有一个对象实例,并提供一个该类的全局访问点。
原型模式(Prototype Pattern)用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

posted @ 2012-03-28 15:29  孙金棚  阅读(1175)  评论(0编辑  收藏  举报