原型模式(prototype pattern)
今天有空学习了一下设计模式-> 原型模式(prototype pattern),记录一下笔记做一个初稿,记录一下该模式的知识要点和一些自己的理解,有不对的记得留言改正,灰常感激。
实现原理:创建指定类型的对象,以该对象为原型通过拷贝(深拷贝 或 浅拷贝,关于拷贝初稿就不说了,有空细致研究一下)方式创建新对象。
结构图(摘的,感谢TerryLee,无量天尊):

适用范围(场景):个人感觉该模式适合在需要动态创建很多同类型的对象,同时对象间的属性有的不相同。比如说游戏中碰到了一队的怪,这队怪中有有一个队长,队长一定比小喽啰强一些,这时创建这一队的怪要使用什么方式?原型模式呗(这个比较适合)。以小怪为原型,拷贝n多个出来,将其中的一个修改体力和攻击力,就成队长了;再比如说画板的调色板,里面有n多的颜色,这些要创建出来也很费劲,如果使用抽象工厂模式(abstract factory)也不是不可以,问题是颜色如果有100来个那就得弄出100来个的该颜色的工厂类,这......体力活啊。还是使用原型模式(prototype pattern)吧。(以上实例据来自院子,感谢,再感谢)
代码实例:
出个例子吧,一个办公室,5个职员,1个boss 用原型模式(prototype pattern)表现一下
1、职员、boss都是人,所以抽象一下,几个属性:职位、姓名,一个方法:显示身份
1 using System;
2
3 /// <summary>
4 ///Humen 干什么的都是人啊
5 /// </summary>
6 [Serializable]
7 public abstract class Human
8 {
9 public Human()
10 {
11 }
12
13 public Human(string Job, string Name)
14 {
15 this._Job = Job;
16 this._Name = Name;
17 }
18
19 private string _Job;
20 private string _Name;
21
22 public string Job
23 {
24 get { return _Job; }
25 set { _Job = value; }
26 }
27
28 public string Name
29 {
30 get { return _Name; }
31 set { _Name = value; }
32 }
33
34 public abstract void Show();
35
36 public abstract Human Clone(bool IsDeepCopy);
37 }
38
2、实现员工特点的人 Worker
1 using System;
2 using System.Collections.Generic;
3 using System.Web;
4 using System.Runtime.Serialization.Formatters.Binary;
5 using System.IO;
6
7 /// <summary>
8 ///Worker 人里面有员工啊,员工也是人啊
9 /// </summary>
10 [Serializable]
11 public class Worker:Human
12 {
13 public Worker()
14 {
15 }
16
17 public Worker(string Job, string Name)
18 : base(Job, Name)
19 { }
20
21 public override void Show()
22 {
23 System.Web.HttpContext.Current.Response.Write(string.Format("职位:{0} 姓名:{1}<br />", this.Job, this.Name));
24 }
25
26 public override Human Clone(bool IsDeepCopy)
27 {
28 Human hu;
29 if (IsDeepCopy)
30 {
31 MemoryStream memoryStream = new MemoryStream();
32 BinaryFormatter formatter = new BinaryFormatter();
33 formatter.Serialize(memoryStream, this);
34 memoryStream.Position = 0;
35 hu = (Worker)formatter.Deserialize(memoryStream);
36 }
37 else
38 hu = (Worker)this.MemberwiseClone();
39 return hu;
40 }
41 }
42
3、弄一个生人的类Mather
1 using System;
2 using System.Collections.Generic;
3
4 /// <summary>
5 ///Mother 的摘要说明
6 /// </summary>
7 public class Mother
8 {
9 public Mother()
10 {
11 //
12 //TODO: 在此处添加构造函数逻辑
13 //
14 }
15
16 public List<Human> CreateDepartHumen(Human hu)
17 {
18 List<Human> depart = new List<Human>();
19 Human w1 = hu.Clone(true) as Human;
20 w1.Job = "经理";
21 w1.Name = "boss";
22 depart.Add(w1);
23 for (int i = 0; i < 5; i++)
24 {
25 Human w = hu.Clone(true);
26 w.Job = "职员";
27 w.Name = "职员姓名" + (i + 1).ToString();
28 depart.Add(w);
29 }
30 return depart;
31 }
32 }
33
4、开始生成了
1 using System;
2 using System.Collections.Generic;
3 using System.Web;
4 using System.Web.UI;
5 using System.Web.UI.WebControls;
6
7 public partial class _Default : System.Web.UI.Page
8 {
9 protected void Page_Load(object sender, EventArgs e)
10 {
11
12
13
14
15
16
17 Human hu = new Worker();
18 Mother mo = new Mother();
19 List<Human> depart = mo.CreateDepartHumen(hu);
20
21 foreach (Worker wo in depart)
22 {
23 wo.Show();
24 }
25 }
26
27 }
浙公网安备 33010602011771号