如何维护 属性 中的状态,需要从(某种)parent/child 关系中的不同 类 访问该状态?
How to maintain a state in a property, which needs to be accessed from different classes in a (kind of) parent/child relationship?
我有一个更复杂的结构,其中一些 "child" class 作为子视图添加到 "parent" class。因此,我有一个名为 currentSelectedDate
的变量,它应该可以在所有连接的 classes 中访问。我当前的结构如下所示:
Class A(最上面"parent"):
private DateTime currentSelectedDate;
public DateTime CurrentSelectedDate {
get
{
if (this.dayHeader != null)
{
return this.dayHeader.CurrentSelectedDate;
}
else
{
return this.currentSelectedDate;
}
}
set
{
this.currentSelectedDate = value;
if (this.dayHeader != null)
{
this.dayHeader.CurrentSelectedDate = value;
}
}
}
Class B(A/dayHeader 的 "child"):
private DateTime currentSelectedDate;
public DateTime CurrentSelectedDate {
get
{
if (this.weekdayScroller != null)
{
return this.weekdayScroller.CurrentSelectedDate;
}
else
{
return this.currentSelectedDate;
}
}
set
{
this.currentSelectedDate = value;
if (this.weekdayScroller != null)
{
this.weekdayScroller.CurrentSelectedDate = value;
}
}
}
Class C ("child" of B / weekdayScroller):
public DateTime CurrentSelectedDate { get; set; }
这里只展示了从父级到子级的方向,这里的属性是用来传播数据的。在向后的方向上,我使用事件。这种方法的缺点是我必须在每个 class 中多次存储值。此外,如果我在一个 class 中,我必须设置 this.currentSelectedDate
的值,而且在另一个 class(例如 dayHeader.CurrentSelectedDate
)中手动设置(setter/getter 只能由外部调用使用)。
对于class A,class C(weekdayScroller)被隐藏。
Class C (weekdayScroller) 也被其他 classes 使用,所以我必须直接在其中维护 属性 currentSelectedDate
.
还必须注意某些东西何时被初始化。例如。在创建 class A 时设置了 currentSelectedDate
,但 class B 尚不存在。因此 null
检查。
我现在的问题是,这是否是处理状态在所有 class 之间传播的好方法,或者是否有更好的方法。
有没有更好的方法?不是我知道的。我理解这种设计的缺点。
如果只有一个变量值要设置(所以父子关系是 1:1),为什么不把它放在一个共享的 class 中,你可以在任何地方引用它。它实际上是相同的,但可能更容易了解发生了什么以及它由谁负责。
我有一个更复杂的结构,其中一些 "child" class 作为子视图添加到 "parent" class。因此,我有一个名为 currentSelectedDate
的变量,它应该可以在所有连接的 classes 中访问。我当前的结构如下所示:
Class A(最上面"parent"):
private DateTime currentSelectedDate;
public DateTime CurrentSelectedDate {
get
{
if (this.dayHeader != null)
{
return this.dayHeader.CurrentSelectedDate;
}
else
{
return this.currentSelectedDate;
}
}
set
{
this.currentSelectedDate = value;
if (this.dayHeader != null)
{
this.dayHeader.CurrentSelectedDate = value;
}
}
}
Class B(A/dayHeader 的 "child"):
private DateTime currentSelectedDate;
public DateTime CurrentSelectedDate {
get
{
if (this.weekdayScroller != null)
{
return this.weekdayScroller.CurrentSelectedDate;
}
else
{
return this.currentSelectedDate;
}
}
set
{
this.currentSelectedDate = value;
if (this.weekdayScroller != null)
{
this.weekdayScroller.CurrentSelectedDate = value;
}
}
}
Class C ("child" of B / weekdayScroller):
public DateTime CurrentSelectedDate { get; set; }
这里只展示了从父级到子级的方向,这里的属性是用来传播数据的。在向后的方向上,我使用事件。这种方法的缺点是我必须在每个 class 中多次存储值。此外,如果我在一个 class 中,我必须设置 this.currentSelectedDate
的值,而且在另一个 class(例如 dayHeader.CurrentSelectedDate
)中手动设置(setter/getter 只能由外部调用使用)。
对于class A,class C(weekdayScroller)被隐藏。
Class C (weekdayScroller) 也被其他 classes 使用,所以我必须直接在其中维护 属性 currentSelectedDate
.
还必须注意某些东西何时被初始化。例如。在创建 class A 时设置了 currentSelectedDate
,但 class B 尚不存在。因此 null
检查。
我现在的问题是,这是否是处理状态在所有 class 之间传播的好方法,或者是否有更好的方法。
有没有更好的方法?不是我知道的。我理解这种设计的缺点。
如果只有一个变量值要设置(所以父子关系是 1:1),为什么不把它放在一个共享的 class 中,你可以在任何地方引用它。它实际上是相同的,但可能更容易了解发生了什么以及它由谁负责。