Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:19:52+00:00 2026-06-12T14:19:52+00:00

I have developed a web service in java which contains about 30 operations. Each

  • 0

I have developed a web service in java which contains about 30 operations. Each operation has the exact same code except for 2-3 lines. Following is a sketch of the code

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;
}

What approach should i follow so that i dont have to write the same code in each operation?

  1. Should i use reflection?
  2. I have heard about Aspect Oriented Programming…can AOP be used here?
  3. Should i go with the plain old switch case statements and a method to decide which method to call based on operation?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:19:54+00:00Added an answer on June 12, 2026 at 2:19 pm

    This looks like a good candidate for the template method pattern. Define an abstract class containing the main method (final), which delegates the specific parts to protected abstract methods.

    In each method of your web service, instantiate a subclass of this abstract class which only overrides the two specific abstract methods, and call the main method of this subclass instance.

    public abstract class Operation {
        public final Response answer(String arg1, String arg2) {
            authorizeSession();
            Response res;
    
            if (authorized) {
                try {
                    Object result = executeSpecificPart(arg1, arg2);
                    res = Response.ok(result).build();
                }
                catch (MultipleExceptions ex) {
                    res = handleExceptions();
                }
                finally {
                    logInDatabase();
                }
            }
    
            return res;
        }
    
        protected abstract Object executeSpecificPart(String arg1, String arg2);
    }
    
    ...
    
        public Response operation1(final String arg1, final String arg2) {
            initialize();
            Operation op1 = new Operation() {
                protected Object executeSpecificPart(String arg1, String arg2) {
                    ...
                }
            };
            return op1.answer();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a soap based web service. It returns java objects which contains
I am using a Java Web Service which is developed by one of our
I am working on a web service for which we have already developed apps
I have a web application which has several users and each user will have
I have developed REST web service which is able to accept some data, load
I have a Restful web service developed in Spring MVC which currently returns farmer
I'm working on a Java 6 application server which has a web service for
I have developed a simple Apache-Axis java web service using Eclipse IDE and now
I have a simple WCF web service on my machine which I have developed
I have developed a web service and host it on my server and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.