在这种情况下可以使用Java Observer和Observable吗?

时间:2017-02-05 22:47:58

标签: java observable observer-pattern

我有一个Android应用程序,我将使用Java ObserverObservable类来实现Observer模式以侦听要更改的变量。我已将问题简化为可编译的Java项目。

扩展Observable

的类
import java.util.Observable;

public class ServerResponse extends Observable {

    String request;
    String response;

    public ServerResponse() {}

    public ServerResponse(String reqquest) {
        //Process HTTP request here.
        //Notify observers that response has changed.
        response = "Sample HTTP response from server goes here.";
        setResponse(response);
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
        setChanged();
        notifyObservers(response);
    }
}

实施Observer

的类
import java.util.Observable;
import java.util.Observer;

public class UserProfileActivity implements Observer {

    public static void main (String[] args) {
        System.out.println("Adding UserProfileActivity as an observer.");
        ServerResponse server = new ServerResponse();
        UserProfileActivity observer = new UserProfileActivity();
        server.addObserver(observer);
    }

    @Override
    public void update(Observable o, Object arg) {
       System.out.println("Response obtained from server: " + o.toString());
    }

}

我遇到的问题是,当我尝试在setResponse中致电ServerResonse.java时,观察员未收到通知......但是当我在setResponse中拨打UserProfileActivity.java时,观察者能够得到通知。这种行为不是我的预期。

我想要的预期行为是ServerResponse类是从Web服务器收到响应后调用setResponse的类(这一切都在ServerResponse中处理) 。

我是否误解了Java的ObserverObservable类?如果Observer是应该收到通知的setResponseServerResponse可以在Observer内调用$query = "SELECT Family.ID, Family.name, (SELECT GROUP_CONCAT( DISTINCT CONCAT_WS('|||', Member.ID, Member.Firstname COALESCE((SELECT GROUP_CONCAT( DISTINCT CONCAT_WS('|', Contribution.ID, Contribution.Amount) SEPARATOR '||') FROM Contribution WHERE Contribution.Member_ID = Member.ID AND Contribution.Family_ID = Family.ID),' '), ' ' ) SEPARATOR '||||' ) FROM Member WHERE Member.Family_ID = Family.ID) as Members FROM Family"; //Execute your query, get back $Families... foreach($Families as $k1=>$Family) { $Family['Members'] = array_filter(explode("||||", trim($Family['Members']))); foreach($Family['Members'] as $k2=>$Member) { $Member = array_filter(explode("|||", trim($Member))); $Member = [ "ID" => $Member[0], "Firstname" => $Member[1], "Contributions" => array_filter(explode("||", trim($Member[2]))) ]; foreach($Member['Contributions'] as $k3=>$Contribution) { $Contribution = array_filter(explode("|", trim($Contribution))); $Contribution = [ "ID" => $Contribution[0], "Amount" => $Contribution[1] ]; $Member['Contributions'][$k3] = $Contribution; } $Family['Members'][$k2] = $Member; } $Families[$k1] = $Family; } ,我感到很困惑。这似乎有点落后于我。

1 个答案:

答案 0 :(得分:0)

我将选择代码:

public ServerResponse(String reqquest) {
    //Process HTTP request here.
    //Notify observers that response has changed.
    response = "Sample HTTP response from server goes here.";
    setResponse(response);
}

这永远不会产生预期的效果。你刚刚构建了一个新的Response类,没有人在任何地方都有这样的引用。所以通知任何人都没有意义,因为没有人能够注册。

public static void main (String[] args) {
    System.out.println("Adding UserProfileActivity as an observer.");
    ServerResponse server = new ServerResponse();
    UserProfileActivity observer = new UserProfileActivity();
    server.addObserver(observer);
}

在此您向server添加观察者。然后,线程结束,程序退出。

添加类似的内容以实际获取更新:

     for(int i = 0; i < 10; i++) {
         server.setResponse("New response " + i);
         Thread.sleep(1000);
     }

现在调用更新方法。