`
收藏列表
标题 标签 来源
封装微博请求时的泛型思考
public interface Command<T> {
	// 处理时用T
	public T execute();
}

public class t_add_command implements Command<Integer> {

	@Override
	public Integer execute() {
		return 1;
	}
}

public class t_del_command implements Command<String> {

	@Override
	public String execute() {
		// TODO Auto-generated method stub
		return "t_del_command";
	}

}



public class RequestService extends Observable {
	private static ExecutorService sExec = Executors.newCachedThreadPool();

	// 传递时用<?>
	public void request(Command<?> cmd) {
		sExec.submit(new Task(cmd));
	}
	
	private class Task implements Runnable {
		private Command<?> cmd;
		
		public Task(Command<?> cmd) {
			this.cmd = cmd;
		}
		
		public void run() {
			notifyObservers(cmd.execute());
		}
	}
}
Global site tag (gtag.js) - Google Analytics