如何使用观察者设计模式? [英] How can I use observer design pattern?
问题描述
我需要使用Observer设计模式进行学校项目,但我遇到问题。
我有一个定时器和一个int,它本身减少,我想让一个观察者,所以当int变成0,一个jdialog弹出。
I need to use the Observer design pattern for a school project, but I'm having problems with it. I have a timer and an int which decrements itself, I want to make an observer so when the int becomes 0, a jdialog pops up.
这里是我的代码:
public class Joc extends JPanel implements ActionListener {
javax.swing.Timer t1;
int time=0,
MyObservator obs = new MyObservator();
public Joc()
{ ... // constructor stuff
t1=new javax.swing.Timer(1000, (ActionListener) this);
t1.start();}
public void actionPerformed(ActionEvent e) {
if(time>0) {time--;}
else {}
}
//我的观察者类
public class MyObservator implements Observer{
Joc joc;
MyJDialog dialog;
@Override
public void update(Observable o, Object arg) {
if(joc.time==0)
{dialog.setVisible(true);}
}
}
我不明白如何使用观察者,如何添加观察者,如何使其成为功能?
I didn't understand how to use the observer, how can I make add the observer and how can I make it function?
推荐答案
p>使用观察者模式,您将从观察者的基本界面开始。当特定事件发生时,观察者通知方法将被其他类调用。
With the observer pattern you start with a basic interface for an observer. The observers notify method will be called by some other class when a particular event occurs.
public interface IObserver {
public void notify(int i);
}
现在界面需要实现:
public class Observer implements IObserver {
@Override
public void notify(int i) {
if(i % 1000 == 0){
System.out.println("One thousand iterations elapsed.");
}
}
}
需要注意。因此,创建一个包含 IObservers
列表的类。请注意, IObserver
可以添加到 Observable
中包含的列表中。如果需要,可以在接口中指定 addObserver
方法,然后由 Observable
实现。还要注意,当特定事件发生时, Observable
类调用 notify
方法。通知 IObserver
这些事件,并可以在发生时执行任何操作。在我们的示例中,如果 int
是1000,我们会在控制台中打印一些文本。
The observable needs something to observe. So a class is created that contains a list of IObservers
. Notice that IObserver
s can be added to the list contained by the Observable
. If desired, the addObserver
method can be specified in an interface which is then implemented by Observable
. Also notice that the Observable
class calls the notify
method when a particular event occurs. The IObserver
is notified of these events and can perform any action when they occur. In our example, if the int
is 1000 we print some text to the console.
public class Observable {
private List<IObserver> observers = new ArrayList<IObserver>();
public void addObserver(IObserver observer){
this.observers.add(observer);
}
public void execute(){
for(int i = 0; i < 10000; i++){
for(IObserver observer: observers){
observer.notify(i);
}
}
}
}
一个程序,我们注册实现 IObserver
与 Observable
实例的实例。
In a program we register instances implementing IObserver
with the Observable
instance.
public static void main(String[] args) {
Observable observable = new Observable();
observable.addObserver(new Observer());
observable.execute();
}
在您的示例中, Joc
class相当于 Obserable
类。应该包含 Observer
的列表,并通过更新
方法发生特定事件时通知他们。
In your example, the Joc
class is equivalent to the Obserable
class. It should contain a list of Observer
s and notify them when a particular event occurs via the update
method.
这篇关于如何使用观察者设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!