템플릿 방법 모드 응용 소기
원래 코드:
public void intercept(InterceptorChain chain) throws Throwable {
boolean con = true;
if(Context.isMe(chain, this)) {
con = beforeInvoke(chain.getTarget(), chain.getMethod(), chain.getArgs());
Context.meFinished();
} else if(allowNesting) {
log("allow", "before", chain);
con = beforeInvoke(chain.getTarget(), chain.getMethod(), chain.getArgs());
} else {
log("prevent", "before", chain);
}
long s = System.nanoTime();
try {
if (con) {
chain.doChain();
}
} catch (Exception e) {
boolean throwEx = true;
if(Context.isMe(chain, this)) {
throwEx = whenInvokeException(e, chain.getTarget(), chain.getMethod(), chain.getArgs());
Context.meFinished();
} else if(allowNesting) {
log("allow", "after", chain);
throwEx = whenInvokeException(e, chain.getTarget(), chain.getMethod(), chain.getArgs());
} else {
log("prevent", "after", chain);
}
if (throwEx) {
throw e;
}
} finally {
if(Context.isMe(chain, this)) {
Object obj = afterInvoke(s,chain.getReturnValue(),chain.getTarget(),chain.getMethod(),chain.getArgs());
chain.setReturnValue(obj);
Context.meFinished();
} else if(allowNesting) {
log("allow", "after", chain);
Object obj = afterInvoke(s,chain.getReturnValue(),chain.getTarget(),chain.getMethod(),chain.getArgs());
chain.setReturnValue(obj);
} else {
log("prevent", "after", chain);
}
}
}
이 부분을 세 번 반복한 걸 알 수 있어요.
if(Context.isMe(chain, this)) {
...
} else if(allowNesting) {
...
} else {
...
}
내가 세 번째 글을 썼을 때 이미 미쳐버렸다. 이것은 이미 몇 줄의 코드가 많고 몇 줄의 코드가 적은 일이 아니다. 나는 앞으로 누군가가 이 코드를 유지할 때 묵묵히 나를 저주할까 봐 두렵다.
템플릿 메서드를 적용하여 다음과 같이 수정합니다.
//
private static abstract class Template {
String type;
public Template(String type) {
super();
this.type = type;
}
boolean intercept(InterceptorChain chain, AbstractInterceptor i,
Exception e,long beginTime) {
boolean r = true;
if (Context.isMe(chain, i)) {
r = doIntercept(chain, i,e,beginTime);
Context.meFinished();
} else if (i.allowNesting) {
i.log("allow", type, chain);
r = doIntercept(chain, i,e,beginTime);
} else {
i.log("prevent", type, chain);
}
return r;
}
abstract boolean doIntercept(InterceptorChain chain,
AbstractInterceptor i,Exception e,long beginTime);
}
// 3 。
private static final Template before = new Template("before") {
boolean doIntercept(InterceptorChain chain, AbstractInterceptor i,
Exception e,long beginTime) {
return i.beforeInvoke(chain.getTarget(), chain.getMethod(), chain.getArgs());
}
};
private static final Template after = new Template("after") {
boolean doIntercept(InterceptorChain chain, AbstractInterceptor i,
Exception e,long beginTime) {
Object obj = i.afterInvoke(beginTime,chain.getReturnValue(),chain.getTarget(),chain.getMethod(),chain.getArgs());
chain.setReturnValue(obj);
return true;
}
};
private static final Template whenException = new Template("whenException") {
boolean doIntercept(InterceptorChain chain, AbstractInterceptor i,
Exception e,long beginTime) {
return i. whenInvokeException(e, chain.getTarget(), chain.getMethod(), chain.getArgs());
}
};
//
public void intercept(InterceptorChain chain) throws Throwable {
boolean doChain = before.intercept(chain, this, null, 0L);
long s = System.nanoTime();
try {
if (doChain) {
chain.doChain();
}
} catch (Exception e) {
if (whenException.intercept(chain, this, e, s)) {
throw e;
}
} finally {
after.intercept(chain, this, null, s);
}
}
코드가 원래보다 적은 것은 아니지만 마음은 상쾌하고 즐겁게 일하는 원칙에 부합된다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.