使用场景:
1、用于扩展一个类的功能或给一个类添加附加职责
2、动态的给一个对象添加功能,这些功能可以动态的撤销
几个重要的角色:抽象产品,抽象装饰器,具体装饰器,基础产品(标配)
代码设计思路:一个顶层的抽象组件,所有装饰器(包含抽象装饰器和具体装饰器)、具体组件(可叫做标配)都是顶层组件的子类,抽象装饰器必须包含以抽象组件为参数的构造方法,具体装饰器复写抽象装饰器的构造方法并在抽象装饰器的基础上进行扩展和增强,
使用方法:在具体组件(标配)基础是一层一层去套具体装饰器
代码地址:https://github.com/WilliamEvan/my-learning/tree/master/decorator
public class Test {
private static final JsonLogger logger = JsonLoggerFactory.getJsonLogger(Test.class);
public static void main(String[] args) {
logger.info("这是json logger");
logger.error("系统错误");
try{
int i = 1 / 0;
}catch(Exception e){
logger.error(e);
e.printStackTrace();
}
}
}
public class JsonLoggerFactory {
public static JsonLogger getJsonLogger(Class clazz){
Logger logger = LoggerFactory.getLogger(clazz);
JsonLogger jsonLogger = new JsonLogger(logger);
return jsonLogger;
}
}
public class JsonLogger extends JsonLoggerDecorator{
public JsonLogger(Logger logger) {
super(logger);
}
@Override
public void info(String s) {
JSONObject jb = new JSONObject();
jb.put("message", s);
logger.info(jb.toJSONString());
}
@Override
public void error(String s) {
JSONObject jb = new JSONObject();
jb.put("error", s);
logger.info(jb.toJSONString());
}
public void error(Exception e){
JSONObject jb = new JSONObject();
jb.put("exception", e.getClass().getName());
jb.put("stacktrace", e.getStackTrace());
logger.info(jb.toJSONString());
}
}
public class JsonLoggerDecorator implements Logger {
protected Logger logger;
public JsonLoggerDecorator(Logger logger) {
this.logger = logger;
}
@Override
public String getName() {
return null;
}
@Override
public boolean isTraceEnabled() {
return false;
}
@Override
public void trace(String s) {
}
@Override
public void trace(String s, Object o) {
}
@Override
public void trace(String s, Object o, Object o1) {
}
@Override
public void trace(String s, Object... objects) {
}
@Override
public void trace(String s, Throwable throwable) {
}
@Override
public boolean isTraceEnabled(Marker marker) {
return false;
}
@Override
public void trace(Marker marker, String s) {
}
@Override
public void trace(Marker marker, String s, Object o) {
}
@Override
public void trace(Marker marker, String s, Object o, Object o1) {
}
@Override
public void trace(Marker marker, String s, Object... objects) {
}
@Override
public void trace(Marker marker, String s, Throwable throwable) {
}
@Override
public boolean isDebugEnabled() {
return false;
}
@Override
public void debug(String s) {
}
@Override
public void debug(String s, Object o) {
}
@Override
public void debug(String s, Object o, Object o1) {
}
@Override
public void debug(String s, Object... objects) {
}
@Override
public void debug(String s, Throwable throwable) {
}
@Override
public boolean isDebugEnabled(Marker marker) {
return false;
}
@Override
public void debug(Marker marker, String s) {
}
@Override
public void debug(Marker marker, String s, Object o) {
}
@Override
public void debug(Marker marker, String s, Object o, Object o1) {
}
@Override
public void debug(Marker marker, String s, Object... objects) {
}
@Override
public void debug(Marker marker, String s, Throwable throwable) {
}
@Override
public boolean isInfoEnabled() {
return false;
}
@Override
public void info(String s) {
}
@Override
public void info(String s, Object o) {
}
@Override
public void info(String s, Object o, Object o1) {
}
@Override
public void info(String s, Object... objects) {
}
@Override
public void info(String s, Throwable throwable) {
}
@Override
public boolean isInfoEnabled(Marker marker) {
return false;
}
@Override
public void info(Marker marker, String s) {
}
@Override
public void info(Marker marker, String s, Object o) {
}
@Override
public void info(Marker marker, String s, Object o, Object o1) {
}
@Override
public void info(Marker marker, String s, Object... objects) {
}
@Override
public void info(Marker marker, String s, Throwable throwable) {
}
@Override
public boolean isWarnEnabled() {
return false;
}
@Override
public void warn(String s) {
}
@Override
public void warn(String s, Object o) {
}
@Override
public void warn(String s, Object... objects) {
}
@Override
public void warn(String s, Object o, Object o1) {
}
@Override
public void warn(String s, Throwable throwable) {
}
@Override
public boolean isWarnEnabled(Marker marker) {
return false;
}
@Override
public void warn(Marker marker, String s) {
}
@Override
public void warn(Marker marker, String s, Object o) {
}
@Override
public void warn(Marker marker, String s, Object o, Object o1) {
}
@Override
public void warn(Marker marker, String s, Object... objects) {
}
@Override
public void warn(Marker marker, String s, Throwable throwable) {
}
@Override
public boolean isErrorEnabled() {
return false;
}
@Override
public void error(String s) {
}
@Override
public void error(String s, Object o) {
}
@Override
public void error(String s, Object o, Object o1) {
}
@Override
public void error(String s, Object... objects) {
}
@Override
public void error(String s, Throwable throwable) {
}
@Override
public boolean isErrorEnabled(Marker marker) {
return false;
}
@Override
public void error(Marker marker, String s) {
}
@Override
public void error(Marker marker, String s, Object o) {
}
@Override
public void error(Marker marker, String s, Object o, Object o1) {
}
@Override
public void error(Marker marker, String s, Object... objects) {
}
@Override
public void error(Marker marker, String s, Throwable throwable) {
}
}
网友评论