我在 Java 中开发了一个 Web 服务,其中包含大约 30 个操作。除了 2-3 行之外,每个操作都有完全相同的代码。以下是代码草图
public Response operation1(String arg1, String arg2)
{
   initialze(); // this line will be different for each operation with different arguments
   authorizeSession();
   Response res;
   if (authorized)
   {
       try 
       {
          Object result = callMethod1(arg1, arg2); // This line is different for each operation
          res = Response.ok(result).build();
       }
       catch( MultipleExceptions ex)
       {
          res = handleExceptions();
       }
       finally
       {
           logInDatabase();
       }
   }
   return res;
}
我应该遵循什么方法,这样我就不必在每个操作中编写相同的代码?
- 我应该使用反射吗?
- 我听说过面向方面的编程……这里可以使用 AOP 吗?
- 我应该使用普通的旧 switch case 语句和一种方法来决定根据操作调用哪个方法吗?