Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

oop - 更改对象时执行方法(OOP)(Execute a method when object is changed (OOP))

I'm learning OOP and trying to write a simple program that will execute some method every time when a specific varible will change.

(我正在学习OOP,并尝试编写一个简单的程序,该程序将在每次特定变量更改时执行某种方法。)

I have two classes:

(我有两节课:)

public class SomeClass {

  private OtherClass object;

  public OtherClass getObject() {
    return this.object;
  }

  public void setObject(OtherClass object) {
    objectChanged();
    this.object = object;
  }

  private void objectChanged() {
    System.out.println("Object has changed");
  }
}


public class OtherClass {

  private int value = 5;

  public int getValue() {
    return this.value;
  }

  public void setValue(int value) {
    this.value = value;
  }
}

The variable objectChanged should be called every time when variable "object" is changed.

(每次更改变量“ object”时,都应调用变量objectChanged。)

My first naive idea was to put the method call inside of set function.

(我的第一个幼稚想法是将方法调用放在set函数内部。)

But what if you change the object after you set it?

(但是,如果在设置对象后更改了对象怎么办?)

Like this:

(像这样:)

SomeClass someObject = new SomeClass();
OtherClass otherObject = new OtherClass();
someObject.setObject(otherObject); //"Object has changed"
otherObject.setValue(10); //nothing happens yet

I need someObject to realize that object stored inside of it changed its value to 10, but how do i do it?

(我需要someObject来实现存储在其中的对象将其值更改为10,但是我该怎么做?)

Is it even possible in OOP?

(在OOP中甚至可能吗?)

  ask by LostBoy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(再次重申:这与范式并不严格相关。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...