设计模式之美:Dynamic Property(动态属性)

索引

  • 别名

  • 意图

  • 结构

  • 参与者

  • 适用性

  • 效果

  • 实现
    • 实现方式(一):Dynamic Property
      的示例实现。

别名

  • 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
7 public Property GetProperty(string propertyName)
8 {
9 return _properties.GetProperty(propertyName);
10 }
11
12 public bool HasProperty(string propertyName)
13 {
14 return _properties.HasProperty(propertyName);
15 }
16
17 public void AddProperty(string propertyName, Property property)
18 {
19 _properties.AddProperty(propertyName, property);
20 }
21
22 public void RemoveProperty(string propertyName)
23 {
24 _properties.RemoveProperty(propertyName);
25 }
26
27 public IEnumerable<Property> GetAllProperties()
28 {
29 return _properties.GetAllProperties();
30 }
31 }
32
33 public class Property
34 {
35 public string Name { get; set; }
36 public string Value { get; set; }
37 }
38
39 public class PropertyList
40 {
41 private PropertyList _parent;
42 private Dictionary<string, Property> _properties
43 = new Dictionary<string, Property>();
44
45 public PropertyList(PropertyList parent)
46 {
47 _parent = parent;
48 }
49
50 public PropertyList Parent
51 {
52 get
53 {
54 return _parent;
55 }
56 }
57
58 public Property GetProperty(string propertyName)
59 {
60 if (_properties.ContainsKey(propertyName))
61 return _properties[propertyName];
62 if (_parent != null && _parent.HasProperty(propertyName))
63 return _parent.GetProperty(propertyName);
64 return null;
65 }
66
67 public bool HasProperty(string propertyName)
68 {
69 return (_properties.ContainsKey(propertyName))
70 || (_parent != null && _parent.HasProperty(propertyName));
71 }
72
73 public void AddProperty(string propertyName, Property property)
74 {
75 _properties.Add(propertyName, property);
76 }
77
78 public void RemoveProperty(string propertyName)
79 {
80 _properties.Remove(propertyName);
81 }
82
83 public IEnumerable<Property> GetAllProperties()
84 {
85 List<Property> properties = new List<Property>();
86
87 if (_parent != null)
88 properties.AddRange(_parent.GetAllProperties());
89
90 properties.AddRange(_properties.Values);
91
92 return properties;
93 }
94 }
95
96 public class Client
97 {
98 public void 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" });
104
105 if (dennis.HasProperty("Contact"))
106 {
107 Property property = dennis.GetProperty("Contact");
108 }
109 }
110 }
111 }

设计模式之美》为 Dennis
Gao
 发布于博客园的系列文章,任何未经作者本人同意的人为或爬虫转载均为耍流氓。

时间: 2024-08-05 07:26:07

设计模式之美:Dynamic Property(动态属性)的相关文章

设计模式之美:Manager(管理器)

索引 意图 结构 参与者 适用性 效果 实现 实现方式(一):Manager 模式的示例实现. 意图 将对一个类的所有对象的管理封装到一个单独的管理器类中. 这使得管理职责的变化独立于类本身,并且管理器还可以为不同的类进行重用. Encapsulates management of a class's objects into a separate manager object. This allows variation of management functionality independ

设计模式之美:Product Trader(操盘手)

索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Product Trader 的示例实现. 意图 使客户程序可以通过命名抽象超类和给定规约来创建对象. Product Trader 让客户程序与 Product 类解耦,从而使得类的层级结构.框架和应用程序易于改写.配置和演进. Let clients create objects by naming an abstract superclass and by providing a specification. A Pr

设计模式之美:Null Object(空对象)

索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Null Object 的示例实现. 意图 通过对缺失对象的封装,以提供默认无任何行为的对象替代品. Encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior. In short, a design where "nothing w

设计模式之美:Role Object(角色对象)

索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Role Object 的示例实现. 意图 通过明确地附加角色对象到目标对象中,以使对象可以适配不同的客户需求.每个角色对象都代表着目标对象在客户上下文中的一种角色.每种上下文都存在于特定的应用程序中,因此可使对象在不同的应用程序间解耦. Adapt an object to different client's needs through transparently attached role objects, each

元类编程--property动态属性

from datetime import date, datetime class User: def __init__(self, name, birthday): self.name = name self.birthday = birthday self._age = 0 # def get_age(self): # return datetime.now().year - self.birthday.year @property #动态属性 def age(self): #属性描述符,g

《设计模式之美》 &lt;02&gt;评判代码质量好坏的维度

如何评价代码质量的高低? 实际上,咱们平时嘴中常说的“好”和“烂”,是对代码质量的一种描述.“好”笼统地表示代码质量高,“烂”笼统地表示代码质量低.对于代码质量的描述,除了“好”“烂”这样比较简单粗暴的描述方式之外,我们也经常会听到很多其他的描述方式.这些描述方法语义更丰富.更专业.更细化.我搜集整理了一下,罗列在了下面.这些几乎涵盖我们所能听到的描述代码质量的所有常用词汇,你可以看一看. 灵活性(flexibility).可扩展性(extensibility).可维护性(maintainabi

设计模式之美

转自:http://www.cnblogs.com/gaochundong/p/design_patterns.html 目录 设计模式分类 设计模式之间的关系 设计模式所支持的设计的可变方面 设计模式怎样解决设计问题 寻找合适的对象 决定对象的粒度 指定对象接口 描述对象的实现 运用复用机制 关联运行时和编译时的结构 设计应支持变化 源代码 设计模式分类 目的 (Purpose) 创建型 (Creational) 结构型 (Structural) 行为型 (Behavioral) 范围 (Sc

【7.1】property动态属性

Python中有一个被称为属性函数(property)的小概念 将类方法转换为只读属性 重新实现一个属性的setter和getter方法 1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 from datetime import date, datetime 4 5 6 class User: 7 def __init__(self, name, birthday): 8 self.name = name 9 self.birthday =

设计模式之美—策略模式

策略模式 什么是策略模式 策略模式的用意是针对一组算法,将每一个算法封装到具有共同接口的独立类中,从而使得它们可以相互替换.策略模式使得算法可以在不影响到客户端的情况下发生变化 策略模式是对算法的包装,是把使用算法的责任和算法本身分开.策略模式通常是把一系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类. 例如下象棋时,炮的前进策略和马.兵的策略是不一样的,算法是有区别的. 我们开始实战,为了说明问题,我们以上图为例做一个超级简单的象棋(简单到没法玩象棋,嘿嘿),假设现在象棋只有炮.兵