设计模式之美: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 }
复制代码








目录
相关文章
|
5月前
|
设计模式
二十三种设计模式全面解析-组合模式与装饰器模式的结合:实现动态功能扩展
二十三种设计模式全面解析-组合模式与装饰器模式的结合:实现动态功能扩展
|
6月前
|
设计模式 Java
设计模式之代理模式(静态&动态)代理
二十三种设计模式中的一种,属于结构型模式。它的作用就是通过提供一个代理类,让我们在调用目标方法的时候,不再是直接对目标方法进行调用,而是通过代理类间接调用。让不属于目标方法核心逻辑的代码从目标方法中剥离出来——解耦。调用目标方法时先调用代理对象的方法,减少对目标方法的调用和打扰,同时让附加功能能够集中在一起也有利于统一维护。
26 0
|
7月前
|
设计模式 Java 数据库连接
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能
|
设计模式 算法 JavaScript
「手摸手设计模式系列」 策略模式与动态表单验证
策略模式 (Strategy Pattern)又称政策模式,其定义一系列的算法,把它们一个个封装起来,并且使它们可以互相替换。封装的策略算法一般是独立的,策略模式根据输入来调整采用哪个算法。关键是策略的实现和使用分离。
「手摸手设计模式系列」 策略模式与动态表单验证
|
C# 设计模式
WPF在在设计模式,使用动态样式
原文:WPF在在设计模式,使用动态样式 1.问题分析 WPF有时候要用到主题样式,比如颜色主题(红色、黄色之类的)通常是key相同,而value不同,比如会这么写: Background="{DynamicResource BackgroundColor}"    主题切换通常在不同的资源文件xaml里面,这时候,如果想在设计时(设计视图)里看看主题,往往得写些临时代码,当编译的时候还得把临时代码删除。
1203 0
|
Java 设计模式
结构型设计模式--代理模式(静态&动态)
代理简述: 代理是一个动词,动词之间会有产生关系两者。代理这个词产生关系的两者就是:realSubject(被代理者),proxySubject(代理者)。
747 0
|
1月前
|
设计模式 SQL 算法
设计模式了解哪些,模版模式
设计模式了解哪些,模版模式
22 0
|
1月前
|
设计模式 Java 数据库
小谈设计模式(2)—简单工厂模式
小谈设计模式(2)—简单工厂模式
|
1月前
|
设计模式 Java PHP
php设计模式--简单工厂模式(一)
php设计模式--简单工厂模式(一)
15 0