想象一下,您需要从整个应用程序中访问一些方法.静态类是理想的.
- public static class MyStaticClass
- {
- public static void MyMethod()
- {
- // Do Something here...
- }
- }
- public static class MyStaticClass2
- {
- public static void MyMethod()
- {
- // Do Something here...
- }
- }
有没有办法改变我的其他代码中使用的静态类而不更改MyStaticClass.MeMethod()的调用;到MyStaticClass2.MyMethod();?
我想到了一个界面,但我不知道如何实现这个……如果我说疯了就说出来,我只会改变电话:D
解决方法
你想要一个
factory pattern
所以你的工厂是
- public static MyStaticClassFactory
- {
- public static IMyNonStaticClassBase GetNonStaticClass()
- {
- return new MyNonStaticClass1();
- }
- }
例
- public class MyNonStaticClass1 : IMyNonStaticClassBase
- {
- //
- }
接口
- public interface IMyNonStaticClassBase
- {
- void MyMethod();
- }