Naor
Naor

Reputation: 24063

add method call to each method in a class

I have class with many methods:

public class A {
    public string method1() {
        return "method1";
    }
    public string method2() {
        return "method2";
    }
    public string method3() {
        return "method3";
    }
    .
    .
    .
    public string methodN() {
        return "methodN";
    }
}

I would like to add call to doSomething() in each method, for example:

public string methodi() {
    doSomething();
    return "methodi";
}

What is the best way to do so? Is there any suitable design pattern?

Upvotes: 8

Views: 3103

Answers (4)

Michael Schmeißer
Michael Schmeißer

Reputation: 3417

Using AOP is already a good answer, it was my first idea too.

I tried to figure out a good way doing it without AOP though and came up with this idea (using the Decorator pattern):

interface I {
  String method1();
  String method2();
  ...
  String methodN();
}

class IDoSomethingDecorator implements I {
  private final I contents;
  private final Runnable commonAction;

  IDoSomethingDecorator(I decoratee, Runnable commonAction){
    this.contents = decoratee;
    this.commonAction = commonAction;
  }

  String methodi() {
    this.commonAction().run();
    return contents.methodi();
  }
}

You could then decorate the construction of A (which implements I):

I a = new IDoSomethingDecorator(new A(),doSomething);

It is basically no rocket science and in fact results in more code than your first idea, but you are able to inject the common action and you separate the additional action from the class A itself. Further, you can turn it off easily or use it only in tests, for instance.

Upvotes: 5

Andreas Dolk
Andreas Dolk

Reputation: 114767

This is a typical use case for AOP (aspect oriented programming). You'd define the insertion points for the method calls and the AOP engine adds the correct code to the class file. This is often used when you want to add log statements without cluttering your source files.

For java you could add the aspectj library

For C# and .NET have look at this blog. Looks like a good starter.

Upvotes: 7

Vaandu
Vaandu

Reputation: 4935

You could use reflection.

public String callMethod(int i) {
  doSomething();  
  java.lang.reflect.Method method;    
  try {
    method = this.getClass().getMethod("method" + i);
  } catch (NoSuchMethodException e) {
    // ...
  }
  String retVal = null;
  try {
    retVal = method.invoke();
  } catch (IllegalArgumentException e) {
  } catch (IllegalAccessException e) {
  } catch (InvocationTargetException e) { }
  return retVal;
}

Upvotes: 0

daryal
daryal

Reputation: 14919

Why not having a single function?

public string methodi(int i) {
    doSomething();
    return "method" + i.toString();
}

Or you may write a function which takes an Func parameter and call this function instead of your functions.

    public string Wrapper(Func<string> action)
    {
        doSomething();
        return action();
    }

and call your functions from this function;

string temp = Wrapper(method1);

Upvotes: 1

Related Questions