JAvaコールバックモード
1228 ワード
/**
* Created by liuxd on 2015-10-23.
*/
public class CallBackTest {
/**
:
...
...
message( )
:
sendMsg-> handler-> callBack
*/
public void test() {
Server server = new Server();
Client client = new Client(server);
client.sendMsg();
}
class Server {
public void handler(CallBack callBack,Object obj){
System.out.println(" ...");
callBack.callBack(obj);
}
}
class Client implements CallBack{
private Server server;
public Client(Server server) {
this.server = server;
}
public void sendMsg(){
System.out.println(" ...");
new Thread(new Runnable() {
@Override
public void run() {
// this
server.handler(Client.this,"message( )");
}
}).start();
}
@Override
public void callBack(Object obj) {
System.out.println(String.format(" %s",obj));
}
}
interface CallBack{
void callBack(Object obj);
}
}