JAVA重复使用并最终尝试返回构建
请看以下代码:JAVA重复使用并最终尝试返回构建,java,try-catch,code-reuse,Java,Try Catch,Code Reuse,请看以下代码: IDfSessionManager manager = DfcSessionManager.getSessionManager(); try { IDfSession session = manager.getSession(DfsUtils.getCurrentRepository()); ... return somewhat; //May be without return statement } finall
IDfSessionManager manager = DfcSessionManager.getSessionManager();
try {
IDfSession session = manager.getSession(DfsUtils.getCurrentRepository());
...
return somewhat; //May be without return statement
} finally {
if (session != null) {
manager.release(session);
}
}
这样的构造会重复多次并围绕不同的代码。这可以是带或不带return语句的方法。
我想让这个try-finally块可以重用
我一直在思考,没有意识到这一点
public abstract class ISafeExecute<T> {
private IDfSession session = null;
protected abstract T execute() throws DfException;
public T executeSafely() throws Exception {
IDfSessionManager manager = DfcSessionManager.getSessionManager();
try {
session = manager.getSession(DfsUtils.getCurrentRepository());
return execute();
} finally {
if (session != null) {
manager.release(session);
}
}
}
public IDfSession getSession() {
return session;
}
您可以使用
Runnable
构建一种机制来实现这一点(类似于将一个函数注入另一个函数):
我做出了这样的决定:
public abstract class SafeExecute<T> {
protected IDfSession session = null;
public T executeSafely() throws Exception {
IDfSessionManager manager = DfcSessionManager.getSessionManager();
try {
session = manager.getSession(DfsUtils.getCurrentRepository());
return logic();
} finally {
if (session != null) {
manager.release(session);
}
}
}
protected abstract T logic() throws Exception;
}
公共抽象类SafeExecute{
受保护的IDfSession会话=空;
public T executeSafely()引发异常{
IDfSessionManager=DfcSessionManager.getSessionManager();
试一试{
session=manager.getSession(DfsUtils.getCurrentRepository());
返回逻辑();
}最后{
if(会话!=null){
经理发布(会议);
}
}
}
受保护的抽象T逻辑()抛出异常;
}
然后通过扩展该类:
public class Service extends SafeExecute<String> {
public String getLoginTicket() throws Exception {
return executeSafely();
}
@Override
protected String logic() throws Exception {
//TODO implement
}
}
公共类服务扩展了SafeExecute{
公共字符串getLoginTicket()引发异常{
返回executeSafely();
}
@凌驾
受保护的字符串逻辑()引发异常{
//待办事项实施
}
}
第一个代码段甚至不编译。您必须在try块外声明会话
,才能在finally-block内使用它!但是是可调用的,而不是可运行的。@Mudu有可调用的-它只返回类型为T的结果,而可运行的doesntSpring使用HibernateTemplate和JdbcTemplate采用这种方法。通常称为“回调”模式。
public void runInSession(Runnable<IDfSession> runnable) {
IDfSession session = null;
try {
session = manager.getSession(DfsUtils.getCurrentRepository());
runnable.run(session);
} finally {
if (session != null) {
manager.release(session);
}
}
}
interface ISafeExecute<T> {
void execute(IDfSession session);
T getResult();
Exception getException();
}
mySafeExecute.execute(session);
if(mySafeExecute.getException() == null) {
return mySafeExecute.getResult();
} else {
// runtime exception or declaration in method
// signature
throw new RuntimeException(mySafeExecute.getException());
}
public abstract class SafeExecute<T> {
protected IDfSession session = null;
public T executeSafely() throws Exception {
IDfSessionManager manager = DfcSessionManager.getSessionManager();
try {
session = manager.getSession(DfsUtils.getCurrentRepository());
return logic();
} finally {
if (session != null) {
manager.release(session);
}
}
}
protected abstract T logic() throws Exception;
}
public class Service extends SafeExecute<String> {
public String getLoginTicket() throws Exception {
return executeSafely();
}
@Override
protected String logic() throws Exception {
//TODO implement
}
}