All pastes #770804 Raw Edit

epswing

public java v1 · immutable
·published 2007-11-12 16:30 UTC
rendered paste body
public abstract class Command<R> {	private SomeManager manager;	protected void setManager(SomeManager mgr) {		this.manager = mgr;	}	public abstract R execute() throws Exception;}public class FindUserCommand extends Command<User> {	private long id;	public FindUserCommand(long id) {		this.id = id;	}	public User execute() throws Exception {		// assume following method call throws UserNotFoundException		return manager.findUserById(id);	}}public class Server {	public <T> T executeCommand(Command<T> command) throws Exception {		command.setManager(mgr);		return command.execute();	}}public class Client {	try {		// assume user with id=42 does not exist and won't be found		Command command = new FindUserCommand(42);		User u = client.getServer().executeCommand(command);	}	catch (UserNotFoundException e) {		// we never get here		e.printStackTrace();	}	catch (Exception e) {		// we DO get here, but...		e.printStackTrace();		// ...is a java.lang.reflect.InvocationTargetException		e.getCause().printStackTrace()		// ...is my precious UserNotFoundException	}}