原型模式
2011-03-06 08:29 杨延成 阅读(264) 评论(0) 编辑 收藏 举报
1namespace Prototype
2{
3 class Program
4{
5 static void Main(string[] args)
6{
7
8 MyPrototype my = new MyPrototype();
9 my.PropertyTest = \"test\";
10
11 my.ShowProperty();
12
13 //未使用管理器
14 //MyPrototype newMy = my.Clone() as MyPrototype;
15 //newMy.ShowProperty();
16
17
18
19///使用管理器
20 PrototypeManager pm = new PrototypeManager();
21 pm = my;
22 pm = new MyPrototype(\"second\");
23 MyPrototype newMy = pm.Clone() as MyPrototype;
24 newMy.ShowProperty();
25
26
27 }
28 }
29
30 public interface IPrototype
31{
32 IPrototype Clone();
33 }
34
35
36#region prototype
37
38///
39 /// 原型
40 ///
41 public class MyPrototype : IPrototype
42{
43 private string propertyTest;
44
45 public string PropertyTest
46{
47 get { return propertyTest; }
48 set { propertyTest = value; }
49 }
50
51 public MyPrototype()
52{
53 this.propertyTest = \"Init String\";
54 }
55 public MyPrototype(string propertyTest)
56{
57 this.propertyTest = propertyTest;
58 }
59
60 public void ShowProperty()
61{
62 Console.WriteLine(propertyTest);
63 }
64
65#region IPrototype Members
66
67 public IPrototype Clone()
68{
69
70///深拷贝
71 ///
72 return new MyPrototype(propertyTest);
73
74///浅拷贝
75 ///
76 // return this.MemberwiseClone() as IPrototype;
77
78 }
79
80 #endregion
81 }
82 #endregion
83
84
85///
86 /// 管理器
87 ///
88 public class PrototypeManager
89{
90 private Dictionary<string, IPrototype> prototypeManager = new Dictionary<string, IPrototype>();
91 public IPrototype this
92{
93{
94 prototypeManager.Add(propertyTest, value);
95 }
96 get
97{
98 return prototypeManager;
99 }
100 }
101 }
102
103}
在以下几种情况中应当采用原型模式来创建对象:
1、对象的构造函数非常复杂,在生成新对象的时候非常耗时间、耗资源的情况,使用原型模式可以简捷地创建新对象。
2、程序运行过程中,由于一些数据随进都在变化,事后很难使用构造函数再构造出一个和原来一样的对象。
3、不知道当前对象的状态,即目前不知道当前对象成员变量的值都变成了什么内容,故无法再用构造函数来构造和当前对象一样的对象了。
2{
3 class Program
4{
5 static void Main(string[] args)
6{
7
8 MyPrototype my = new MyPrototype();
9 my.PropertyTest = \"test\";
10
11 my.ShowProperty();
12
13 //未使用管理器
14 //MyPrototype newMy = my.Clone() as MyPrototype;
15 //newMy.ShowProperty();
16
17
18
19///使用管理器
20 PrototypeManager pm = new PrototypeManager();
21 pm = my;
22 pm = new MyPrototype(\"second\");
23 MyPrototype newMy = pm.Clone() as MyPrototype;
24 newMy.ShowProperty();
25
26
27 }
28 }
29
30 public interface IPrototype
31{
32 IPrototype Clone();
33 }
34
35
36#region prototype
37
38///
39 /// 原型
40 ///
41 public class MyPrototype : IPrototype
42{
43 private string propertyTest;
44
45 public string PropertyTest
46{
47 get { return propertyTest; }
48 set { propertyTest = value; }
49 }
50
51 public MyPrototype()
52{
53 this.propertyTest = \"Init String\";
54 }
55 public MyPrototype(string propertyTest)
56{
57 this.propertyTest = propertyTest;
58 }
59
60 public void ShowProperty()
61{
62 Console.WriteLine(propertyTest);
63 }
64
65#region IPrototype Members
66
67 public IPrototype Clone()
68{
69
70///深拷贝
71 ///
72 return new MyPrototype(propertyTest);
73
74///浅拷贝
75 ///
76 // return this.MemberwiseClone() as IPrototype;
77
78 }
79
80 #endregion
81 }
82 #endregion
83
84
85///
86 /// 管理器
87 ///
88 public class PrototypeManager
89{
90 private Dictionary<string, IPrototype> prototypeManager = new Dictionary<string, IPrototype>();
91 public IPrototype this
92{
93{
94 prototypeManager.Add(propertyTest, value);
95 }
96 get
97{
98 return prototypeManager;
99 }
100 }
101 }
102
103}
在以下几种情况中应当采用原型模式来创建对象:
1、对象的构造函数非常复杂,在生成新对象的时候非常耗时间、耗资源的情况,使用原型模式可以简捷地创建新对象。
2、程序运行过程中,由于一些数据随进都在变化,事后很难使用构造函数再构造出一个和原来一样的对象。
3、不知道当前对象的状态,即目前不知道当前对象成员变量的值都变成了什么内容,故无法再用构造函数来构造和当前对象一样的对象了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述