It looks to be reasonable, but one should consider many things.
(看起来是合理的,但是应该考虑很多事情。)
This is why there is no automatic way to do it in general. (这就是为什么通常没有自动方法的原因。)
It is not part of the OOP paradigm as such. (因此,它不是OOP范式的一部分。)
If this would be some automatic behavior, it would cause huge overhead as it is not often needed to observe changes this way. (如果这是某种自动行为,则会引起巨大的开销,因为通常不需要以这种方式观察更改。)
But you can, of course, implement your way depending on your concrete requirements. (但是,您当然可以根据自己的具体要求来实现自己的方式。)
There are at least two approaches.
(至少有两种方法。)
In MVVM (like WPF) there is an INotifyPropertyChanged
interface (let's call it a pattern) you can use to trigger a notification yourself, mutch like you did with SomeClass
.
(在MVVM(如WPF)中,有一个INotifyPropertyChanged
接口(我们称其为模式),您可以用来触发通知,就像使用SomeClass
。)
However when you are nesting objects, you need to wire up that mechanism.to cascade: you should do the same with OtherClass
and also connect the actual instances to bubble up changes. (但是,当您嵌套对象时,需要连接该机制以进行级联:您应该对OtherClass
进行相同的OtherClass
,还应将实际实例连接起来以OtherClass
更改。)
See: https://rehansaeed.com/tag/design-patterns/ (参见: https : //rehansaeed.com/tag/design-patterns/)
An other option is the Observable
pattern.
(另一种选择是Observable
模式。)
Each time the object changes state, you emit an instance - the current instance. (每次对象更改状态时,都会发出一个实例-当前实例。)
However, you should care to emit unmutable objects. (但是,您应该注意发出不可变的对象。)
At least by using an interface that makes it read-only. (至少通过使用使其成为只读的接口。)
But you still need to wire up the object tree to react to the changes of nested objects. (但是您仍然需要连接对象树以对嵌套对象的更改做出反应。)
If your platform supports reflection, and you create a proper toolset, you could make this wiring up quite simple.
(如果您的平台支持反射,并且您创建了适当的工具集,则可以使这种连接非常简单。)
But again: this is not strictly related to the paradigm. (再次重申:这与范式并不严格相关。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…