简体   繁体   English

Java中的观察者设计模式

[英]observer design pattern in java

I have the following notification sent for an Observer and Observable design pattern 我已为观察者和可观察的设计模式发送了以下通知

private void changeEnvironment()
    {
        Random random = new Random();
        season = SEASON[random.nextInt(3)];
        dayInTime = DAY_IN_TIME[random.nextInt(2)];
        temperature = random.nextInt(120);
        setChanged();
        notifyObservers(season);
        notifyObservers(dayInTime);
        notifyObservers(temperature);
    }

However, in my: 但是,在我的:

public void update(Observable o, Object arg) {

    }

How do I check whether it's season, dayInTime, or temperature? 如何检查季节,dayInTime或温度?

As per your code, the arg in update(Observable o, Object arg) will be one of season, dayInTime or temperature. 根据您的代码, update(Observable o, Object arg)arg update(Observable o, Object arg)将是季节,dayInTime或温度之一。 The argument to notifyObservers() is what gets passed to update() . notifyObservers()的参数是传递给update()

You probably want to include more metadata in a custom 'event' object and use that as an argument for your calls to notifyObservers() . 您可能希望在自定义“事件”对象中包含更多元数据,并将其用作调用notifyObservers()

You can use the observer pattern in pull mode , which means that observable will pass itself to the observer 's update() .So , observer can get properties they want from the observable 's getters. 您可以在pull模式下使用观察者模式,这意味着observable会将自身传递给观察者的update() 。因此,观察者可以从observable的getter获取所需的属性。

I assume temperature , dayInTime , season in your subject are both int 我假设您的主题中的temperaturedayInTimeseason都为int

So , you Observable (ie Subject) will looks like : 因此,您的Observable (即主题)将如下所示:

public class Subject extends Observable{

    private int temperature;
    private int dayInTime;
    private int season;

    private void changeEnvironment() {
        Random random = new Random();
        this.season = SEASON[random.nextInt(3)];
        this.dayInTime = DAY_IN_TIME[random.nextInt(2)];
        this.temperature = random.nextInt(120);
        setChanged();

         /** Notify all of the subscribed observers about its changes
          It will call each observers' update(this, null)***/
        notifyObservers();
    }


    public int getTemperature(){....}
    public int getdayInTime(){.....}
    public int getSeason(){....}

}

Then your observer can get temperature, dayInTime , season by using the corresponding getters of your Observable : 然后,您的观察者可以通过使用Observable的相应吸气剂来获取温度dayInTime和season:

public void update(Observable o, Object arg) {

   Subject = (Subject) o;
   int temperature = o.getTemperature();
   int dayInTime= o.getdayInTime();
   int season= o.getSeason();
}

You can call notifyObservers("season"); 您可以调用notifyObservers("season"); and in the Observer's update(Observable o, Object arg) , the second parameter will be the string "season" . 在观察者的update(Observable o, Object arg) ,第二个参数将是字符串"season"

Or declared an enum MyAttr {Season, DayInTime, Temperature} and call notifyObservers(MyAttr.Season); 或声明一个enum MyAttr {Season, DayInTime, Temperature}并调用notifyObservers(MyAttr.Season);

If season , dayInTime and temperature should be send to observer at the same time, it may be better to call: 如果应将seasondayInTimetemperature同时发送给观察者,则最好调用:

notifyObservers(season, dayInTime, temperature)

and Observer can have a method update(Object season, Object dayInTime, Object temperature) , now you know whether it's season, dayInTime, or temperature. Observer可以有一个update(Object season, Object dayInTime, Object temperature)方法update(Object season, Object dayInTime, Object temperature) ,现在您知道它是季节,dayInTime还是温度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM