我有复杂的对象模型(涉及大量继承/组合)。它的一些属性可以作为主题的一部分。 给定一个主题,我想初始化它的各种属性。
我该如何设计通用主题类?
是否有任何设计模式可以支持这一点?
最佳答案
主题对我来说听起来像是视觉属性的集合,例如字体大小、背景颜色等。您的复杂对象模型几乎听起来像一个控件/UI 元素层次结构,其中每个控件/元素都具有您希望 Theme 类具有的属性初始化。
您需要做的是让复杂对象模型中的每个对象都可以主题化,因此我们创建 IThemeable:
interface IThemeable
{
void UpdateThemeableProperties();
}
为了让 UpdateThemeableProperties() 更有意义,让我们在一个复杂对象中实现它。
class AComplexObject implements IThemable
{
public bool Selected; // a primitive property we want themeble
public AnotherComplexObject anotherComplexObject; // implements IThemeable
public List<IThemable> Children;
// your object may have other themeable and non-themeable properties.
public AComplexObject()
{
Children = new List<IThemable>();
UpdateThemableProperties();
}
public void UpdateThemableProperties()
{
if (App.CurrentTheme != null)
{
// take care of your primitive properties first by practice
object valueFromTheme = App.CurrentTheme.getPropertyValue("AComplexObject,"Selected");
Selected = (valueFromTheme != null) ? valueFromTheme : false;
// false would be the default value if the theme didn't provide it.
// Do the same as above for all primitive properties you want to
// participate in the theme.
// Do for each individual complex property
if(anotherComplexObject != null)
{
anotherComplexObject.UpdateThemeableProperties();
}
// Do for each of your lists of complex objects:
for each (IThemable it in Children)
{
it.UpdateThemeableProperties();
}
}
}
}
App 将是一个全局静态类,而 CurrentTheme 将是其成员之一。我们创建函数 UpdateThemeableProperties() 因为它被调用很多次,即使在一个类中也是如此。
在 UpdateThemeableProperties 中,我们使用函数 CurrentTheme.getPropertyValue(className,propertyName) 查找当前主题的实现类和属性组合的值,其中 className 和 propertyName 都是字符串。 (是的,我们硬编码了类名和属性名)。该函数返回一个对象类型,尽管实际类型与您正在设置的属性的类型匹配。另外,请记住提供默认值,以防 CurrentTheme 未指定默认值。
请注意,复杂对象模型中任何事物的每个复杂属性都是一个 IThemable。这就是为什么我们可以对所有属性调用 UpdateThemeableProperties(),甚至是那些位于 List 结构上的属性。
现在,当前主题:
class CurrentTheme
{
private Dictionary<String,Object> values;
public CurrentTheme()
{
values = new Dictionary<String,Object>();
}
public void setPropertyValue(string className, string propertyName, Object value)
{
values.add(className + "_" + propertyName, value);
}
public Object getPropertyValue(string className, string propertyName)
{
if (values.keyExists(className + "_" + propertyName)
{
return values[className + "_" + propertyName];
}
else
{
return null;
}
}
}
请注意,您的语言对 Dictionary 的实现可能有所不同。将上面的代码视为伪代码。
在初始化阶段,您可能需要设置主题。在稍后阶段,您通常会构建/实例化复杂模型。复杂模型的实例化可能已从当前主题中找到其值,但实例化后,调用 YourRootMostComplexObject.UpdateThemeableProperties() 来刷新并确保所有子级将从当前主题中获取其属性。
当您更改主题时(例如,通过从下拉列表中选择另一个主题),您将需要再次调用 YourRootMostComplexObject.UpdateThemeableProperties() 来更新所有内容。
在运行时添加子项(如向列表添加项目)时,调用 child.UpdateThemeableProperties():
public addChild(SomeComplexObjectThatImplementsIThemeable child)
{
Children.add(child);
child.UpdateThemeableProperties();
}
那是一篇相当长的帖子。综上所述,
- 复杂对象模型中具有主题的每个类 属性必须实现 IThemeable。
- 请记住实现每个 IThemeable 类的 UpdateThemeableProperties()。在尝试从当前主题获取其值之前,您需要知道哪些属性是可主题化的。
- 在当前主题更改后、在列表中添加 IThemeable 项以及实例化实现 IThemeable 的类型的属性时,会在 IThemeable 类的构造函数中调用 UpdateThemeableProperties()。
关于java - 如何根据给定的主题类型初始化复杂对象模型的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9365008/