设计模式:许多方法共享相同的第一步
是否有一种设计模式可以帮助我避免在许多方法中重复 DoThisStepFirst()
?
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
}
}
编辑1:可以说,这是一个人为的示例。
我的实际实现包括带有参数的方法签名以及每个方法中的重要操作。
编辑2:
- @Marc Gravell 建议面向方面编程。是的,这可能对我有帮助。
- 我还认为责任链模式也可能对我有帮助。
@Charlie Martin 想了解有关该计划的更多信息。 这就是我实际上想做的事情。
- 我正在尝试设置一个测试工具来针对“m”控制器和“n”控制器方法的 ASP.NET MVC 2 应用程序运行。
- MVC 应用程序与 SessionState 紧密耦合。由于各种原因,我无法模拟 SessionState。
- 因此,我想在
DoThisStepFirst()
方法内部初始化 SessionState。 - 初始化 SessionState 后,我想继续执行指定的方法(这就是为什么我怀疑我可能正在寻求“责任链”设计模式)。
- 因此,我想在
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 Gravell 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).
- So inside of the
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果它是测试工具,那么您的单元测试框架应该有一个 TestFixtureSetup 方法,它将在您的测试运行之前首先运行:
对于 NUnit:
对于 MSTest:
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:
For MSTest:
我想您可能有一个始终调用
DoThisStepFirst()
然后执行传递的操作的方法:I guess you could have a method that always calls
DoThisStepFirst()
then performs the passed action:老实说,如果不了解更多有关该程序的信息,就无法回答这个问题。看起来“DoThisStepFirst”是某种初始化;很容易想到
等等,但似乎每个方法都需要初始化。那么问题是“为什么每个方法都处于状态中,所以你需要不断重新初始化?”
从另一个方向来看,您可以想象程序中您确实必须在每个步骤之前进行一些初始化,并且这是相同的初始化,在这种情况下您会陷入困境。
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
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.
将代码放在构造函数中的 DoThisStepFirst() 中怎么样?如果他们所有人都需要它运行才能在那里初始化,那么它很有可能会进入那里。除非它重置了在您的方法执行任何操作之前需要重置的状态/行为。
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?