如何观察自定义对象即使在扩展后也发生变化
假设我在 java 中有一个自定义对象:
public class TestObject {
private int value = 0;
public TestObject(int value) {
this.value = value;
}
public void increaseValue() {
value++;
}
}
现在我想知道这个对象何时被修改。或者更具体地说,我想知道它的任何字段何时发生更改(在本例中为值)。此外,如果我扩展 TestObject,我仍然希望能够侦听该对象可能发生的任何字段更改,包括该更改是否针对新字段新字段。
我做了一些研究,发现了 java 附带的各种监听器,但它们似乎都在要求您在方法末尾调用监听器的地方失败了。例如,increaseValue() 还必须通知 TestObject 的所有侦听器值已更改。显然这对我不起作用,因为可扩展性是必须具备的,而且我不知道从该对象继承的人是否会遵守必须通知侦听器的要求。更不用说,必须为每个变异器方法进行编程似乎是一件痛苦的事情,
如果可以对这个主题有任何启发,我们将不胜感激。
So let's say I have a custom object in java:
public class TestObject {
private int value = 0;
public TestObject(int value) {
this.value = value;
}
public void increaseValue() {
value++;
}
}
Now I want to know when this object is modified. Or more specifically, I want to know when any of it's fields have changed (in this case value). Furthermore, if I extend TestObject, I still want to be able to listen to any field changes that might happen to that object, including if that change is to a new field new fields.
I've done some research and found of variety of listeners that come with java, but they all seem to fail in the area that they require you to put calls to the listeners at the end of you're methods. For example, increaseValue() would also have to notify all of the listeners to the TestObject that value had changed. Obviously this doesn't work for me because extensibility is a must have and I do not know that if people who inherit from that object will adhere to the requirement that they must notify listeners. Not to mention, it seems like a pain to have to program that for each mutator method
If any light could be shed on this subject it would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用Hibernate使用的方法即仪器/代理您的用于在 POJO 的 getter 和 setter 周围添加附加(侦听器)逻辑的类。但是您需要确保所有这些都只使用代理类。
You can use approach that Hibernate uses i.e. instrument/proxy your classes to add additional (listener) logic around your POJOs' getters and setters. But then you need to ensure that all use only proxied classes.
查看AspectJ 项目。您可以使用 切入点进行字段分配,在生产方面非常轻松地实现这一点。
Take a look at The AspectJ Project. You could achieve this very easily with production aspect using a pointcut for field assignment.
您的方法不正确。
您不能强制派生类保持基类的不变式,因为继承可能允许后代类以某种方式更改实现,从而使它们从父类的角度来看无效。
在您的情况下,派生类可能或可能不会在修改时调用通知。
在这种情况下,您应该围绕
Composition
建模,而不是Inheritence
组合与继承
例如:
值知道如何自我增值。
所以所有的代码如下:
所以现在的问题是对象的使用是通过持有者进行的。
然后就会发生通知。
Your approach is not in the correct path.
You can not enforce a derived class to keep the base's class invariants since inheritance can allow descendant classes to alter implementation in a way that makes them invalid from the viewpoint of the parent class.
In your case the derived class MAY or MAY not call the notification upon modification.
In cases like this you should model arround
Composition
and notInheritence
Composition vs Inheritence
For your example:
Value knows how to increment itself.
So all the code will be as follows:
So the catch now is that the usage of the objects are via the holder.
Then the notification will happen.