开发者

Design Patterns: Many Methods Share The Same First Step

Is there a design pattern that can help me avoid repeating DoThisStepFirst() in many methods?

class Program
{
    static void Main(string[] args)
    {
        Method1();
        Method2();
        MethodN();
    }

    private static void DoThisStepFirst()
    {
        // Implementation
    }

    private static void Method1()
    {
        DoThisStepFirst();
        // Implementation
    }

    private static void Method2()
    {
        DoThisStepFirst();
        // Implementation
    }

    // {...}

    private static void MethodN()
    {
        DoThisStepFirst();
        // Implementation
    }
}

EDIT 1: Suffice it to say, this is a contrived example.

My actual implementation includes method signatures with parameters and non-trivial operations in each method.

EDIT 2:

  • @Marc Grav开发者_Python百科ell suggested Aspect Oriented Programming. Yes, that might help me here.
  • I'm also thinking that the Chain-of-Responsibility pattern might help me here as well.

@Charlie Martin wanted to know more about the program. So here's what I'm actually trying to do.

  • I'm trying to set up a test harness to run against an ASP.NET MVC 2 application for 'm' controllers and 'n' controller methods.
  • The MVC application is tightly coupled with SessionState. For various reasons, I can't mock SessionState.
    • So inside of the DoThisStepFirst() method, I'd like to initialize SessionState.
    • And after initializing SessionState, I'd like to proceed to a specified method (which is why I suspect that I might be seeking the 'Chain of Responsibility' design pattern).


If it's a Test Harness, then your Unit Test framework should have a TestFixtureSetup method, it'll run first before your tests are run:

For NUnit:

[TestFixtureSetUp]
public void Setup()
{

}

For MSTest:

[TestInitialize()]
public void Startup()
{
    //Do setup here
}


I guess you could have a method that always calls DoThisStepFirst() then performs the passed action:

    private static void DoSomething(Action doSomethingElse)
    {
        DoThisStepFirst();
        doSomethingElse();
    }


private static void MethodCommon(Func f)
{
    DoThisStepFirst();
    f();
}

private static void Method1()
{
    MethodCommon(() => 
        doSomething();
    );
}


To be honest, there's no way to answer this without knowing more about the pgram. It looks like "DoThisStepFirst" is some kind of initialization; it's tempting to think of

  DoThisStepFirst();
  Method1(); // and so on

and so on, but it appears each method needs the initialization. So then the question is "why is each method piddling in the state so you need to keep reinitializing?"

Going the other direction, you can imagine programs in which you really must do some initialization befre each step, and it's the same initialization, in which case you're pretty well stuck.


How about put the code in DoThisStepFirst() it in the constructor. If all of them need it to run in order to init there, operations there's a good chance it could go in there. Unless it resets states/behaviors that need to be reset before your methods execute anything.


No, there generally is not. Maybe you could provide some details about what you are trying to accomplish?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜