Skip to content

Instantly share code, notes, and snippets.

@wsky
Last active December 17, 2015 16:49
Show Gist options
  • Save wsky/5641097 to your computer and use it in GitHub Desktop.
Save wsky/5641097 to your computer and use it in GitHub Desktop.
context base service design, resolve special runtime/environment dependency
// context interface to resolve env dependency
public interface ContextService {
public Object get(String key);
}
// context base
// alose can impl via aop inspector
public class BizServiceAdapter implements BizInterface {
private ContextService context;
private BizService bizService;
public BizServiceAdapter(ContextService context, BizService bizService) {
this.context = context;
this.bizService = bizService;
}
public String echo(String input) {
if (context.get("appkey") == null)
throw new NullPointerException("Not Auth");
return this.bizService.echo(input);
}
}
// origin service
public class BizService implements BizInterface {
public String echo(String input) {
return input;
}
}
public interface BizInterface {
public String echo(String input);
}
//runtime/env based context impl
public class RemotingContextService implements ContextService {
@Override
public Object get(String key) {
// can get from env-var/threadlocal/per-request
return System.getProperty(key);
}
}
public class HttpContextService implements ContextService {
@Override
public Object get(String key) {
return System.getProperty(key);
}
}
public class HsfContextService implements ContextService {
@Override
public Object get(String key) {
return System.getProperty(key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment