user1193134
user1193134

Reputation: 163

Method to execute commands

I have a about 100 calls of external methods from my own code.

Now I try to perform some actions before and after one of those methods is called.

Is there a way to define a method with an argument containing an other method/block of code, that should be performed? I know this would be highly risky, but overloading 100 of methods within a code I'm not able to edit is also no fun.

Any help would be appreciated

Thanks

Upvotes: 0

Views: 72

Answers (3)

user1193134
user1193134

Reputation: 163

    todo(System.out.println("t"));

    public void todo(String a){
    runtime.exec(a);
    }

Upvotes: 0

fge
fge

Reputation: 121760

Is there a way to define a method with an argument containing an other command that should be performed?

Well, yes. For instance:

public interface Command
{
    void doSomething();
}

then you could call your method with an argument of type Command. All you "have to do" is provide implementations of this interface. For instance:

public void doCommand(final Command command)
{
    command.doSomething();
}

Upvotes: 0

stivlo
stivlo

Reputation: 85506

It seems a cross cutting concern, you should look into AOP (Aspect Oriented Programming).

The Spring Framework allows to implement before advice, after advice and around advice, which seems what you need.

Upvotes: 3

Related Questions