设计模式之美:Dynamic Property(动态属性)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了设计模式之美:Dynamic Property(动态属性),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3197字,纯文字阅读大概需要5分钟。
内容图文
索引
- Property
- Properties
- Property List
使对象可以为客户提供广泛且可扩展的属性集合。
Lets an object provides a generic and extensible set of properties to clients.
Object
- 目标对象可存储 Property 列表。
- 可使用不同的类型来作为 Property 的标识符,最简单的可以使用 string 类型。
Property
- 属性定义。
当以下情况成立时可以使用 Dynamic Property 模式:
- 当对象需要定义大量属性时。
- 当对象的属性是运行时可变时。
- 可在运行时动态的修改对象的属性。
实现方式(一):Dynamic Property 的示例实现。
1 namespace DynamicPropertyPattern.Implementation1 2 { 3 public class Person 4 { 5 private PropertyList _properties = new PropertyList(null); 6 7public Property GetProperty(string propertyName) 8 { 9return _properties.GetProperty(propertyName); 10 } 11 12publicbool HasProperty(string propertyName) 13 { 14return _properties.HasProperty(propertyName); 15 } 16 17publicvoid AddProperty(string propertyName, Property property) 18 { 19 _properties.AddProperty(propertyName, property); 20 } 21 22publicvoid RemoveProperty(string propertyName) 23 { 24 _properties.RemoveProperty(propertyName); 25 } 26 27public IEnumerable<Property> GetAllProperties() 28 { 29return _properties.GetAllProperties(); 30 } 31 } 32 33publicclass Property 34 { 35publicstring Name { get; set; } 36publicstring Value { get; set; } 37 } 38 39publicclass PropertyList 40 { 41private PropertyList _parent; 42private Dictionary<string, Property> _properties 43 = new Dictionary<string, Property>(); 44 45public PropertyList(PropertyList parent) 46 { 47 _parent = parent; 48 } 49 50public PropertyList Parent 51 { 52get 53 { 54return _parent; 55 } 56 } 57 58public Property GetProperty(string propertyName) 59 { 60if (_properties.ContainsKey(propertyName)) 61return _properties[propertyName]; 62if (_parent != null && _parent.HasProperty(propertyName)) 63return _parent.GetProperty(propertyName); 64returnnull; 65 } 66 67publicbool HasProperty(string propertyName) 68 { 69return (_properties.ContainsKey(propertyName)) 70 || (_parent != null && _parent.HasProperty(propertyName)); 71 } 72 73publicvoid AddProperty(string propertyName, Property property) 74 { 75 _properties.Add(propertyName, property); 76 } 77 78publicvoid RemoveProperty(string propertyName) 79 { 80 _properties.Remove(propertyName); 81 } 82 83public IEnumerable<Property> GetAllProperties() 84 { 85 List<Property> properties = new List<Property>(); 86 87if (_parent != null) 88 properties.AddRange(_parent.GetAllProperties()); 89 90 properties.AddRange(_properties.Values); 91 92return properties; 93 } 94 } 95 96publicclass Client 97 { 98publicvoid TestCase1() 99 { 100 Person dennis = new Person(); 101 dennis.AddProperty("Contact", new Property() { Name = "Contact", Value = "Beijing" }); 102 dennis.AddProperty("Age", new Property() { Name = "Age", Value = "18" }); 103 dennis.AddProperty("Gender", new Property() { Name = "Gender", Value = "Male" }); 104105if (dennis.HasProperty("Contact")) 106 { 107 Property property = dennis.GetProperty("Contact"); 108 } 109 } 110 } 111 }
《设计模式之美》为 Dennis Gao 发布于博客园的系列文章,任何未经作者本人同意的人为或爬虫转载均为耍流氓。
原文:http://www.cnblogs.com/gaochundong/p/design_pattern_dynamic_property.html
内容总结
以上是互联网集市为您收集整理的设计模式之美:Dynamic Property(动态属性)全部内容,希望文章能够帮你解决设计模式之美:Dynamic Property(动态属性)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。