vendredi 16 octobre 2015

Is there any design pattern to execute same method differently based on context of execution

I am refactoring some code, and what I want is to let that method execute extra operations based on context in which it is executing.

For example: if current method is as follows

public void func1() {

 /// Operation Set 1

 /// Operation Set 2
 // Operation Set 3
 // Operation Set 4

}

I want it to be something like this:

// After refactoring
punlic void func1() {

/// Operation Set 1

if( context == FULL || context == EXEC_REQUIRED )
{
   /// Operation Set 2

    if( context === EXEC_REQUIRED )
    {
       // Operation Set 3
       // Operation Set 4
    }
}

}

So for this I am just wondering if there is any design pattern already?

Currently I can think about 2 solutions 1. Pass context as parameter to method from caller, but if I follow this then I will have to make changes to all the functions in call stack, but those function may not use this context. 2. Define something like ContextProvider as a static class. And whenever we need context info, ask ContextProvider about it. Problem here is, application is multi threaded , so static doesnt seem right idea.

If you have any solution, that will be helpful.

Thanks, Kailas

Aucun commentaire:

Enregistrer un commentaire