我有一个Android应用程序,我将使用Java Observer
和Observable
类来实现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的Observer
和Observable
类?如果Observer
是应该收到通知的setResponse
,ServerResponse
可以在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;
}
,我感到很困惑。这似乎有点落后于我。
答案 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);
}
现在调用更新方法。