简体   繁体   English

C#中的静态/单个接口实现类

[英]Static/singleton interface implementing class in C#

i have a class that ( right now ) is static: 我有一个( 现在 )是静态的类:

public static class Grob
{
    public static void Frob()
    {
        Foo.Bar();
    }
}

And that works well. 而且效果很好。 Code calls: 代码调用:

Grob.Frob();

and all is right with the world. 世界一切都正确。 Now i want my class to implement an interface: 现在我希望我的班级实现一个接口:

public static class Grob : IOldNewGrob
{
    public static void Frob()
    {
       Foo.Bar();
    }
}

Unfortunately that does not work, because reasons . 不幸的是,由于原因 ,这不起作用。

So i would try changing to class to a singleton : 所以我会尝试将类更改为单例

public sealed class Grob
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Grob() {}

   public static Singleton Instance
   {
      get 
      { 
         if (instance == null) 
         {
            lock (syncRoot) 
            {
               if (instance == null) 
                  instance = new Singleton();
            }
         }
         return instance;
     }
   }       
}

Which works well enough, except that it doesn't work - the code no longer compiles: 除了行不通之外,它行之有效-代码不再编译:

Grob.Frob();

In other languages it would not be a problem. 用其他语言,这将不是问题。 i would create a global Grob function (called Grob because that's the name that existing code needs): 我将创建一个全局Grob函数(称为Grob因为这是现有代码所需的名称):

function Grob(): GrobSingleton;
{
   return Grob.Instance;
}

//and rename Grob class to something else
public sealed class GrobSinglton
{
   ...
}

except that C# doesn't have global functions. 除了C#没有全局功能。

In the end: 到底:

  • i don't need a global function 不需要全局功能
  • i don't need a static class to be able to implement an interface 不需要静态类就能实现接口
  • i don't need a singleton 不需要单身人士

i just want it all to work. 我只希望一切正常。

Why not just create a singleton which also has a static Frob method? 为什么不创建一个单身有一个静态Frob方法?

public sealed class Grob : IOldNewGrob
{
    private static readonly Grob instance = new Grob();

    public static Grob Instance { get { return instance; } }

    // Prevent external instantiation
    private Grob() {}

    public static void Frob()
    {
        Foo.Bar();
    }

    // Implement IOldNewGrob here
}

You should probably also read my article on implementing the singleton pattern in .NET - there's really no need to implement the fragile double-checked locking pattern. 您可能还应该阅读我有关在.NET中实现单例模式的文章 -确实不需要实现易碎的双重检查锁定模式。

That satisfies both of your requirements of making Grob.Frob() work, and making Grob implement an interface. 这满足了使Grob.Frob()工作并使Grob实现接口的两个要求。 It's not clear whether those are your only requirements though - you haven't really explained why you're trying to do that - where the singleton or the interface come in. 尚不清楚这些是否是您唯一的要求-您尚未真正解释为什么要这样做-单例或接口进入的位置。

EDIT: If the idea was that Frob was a member of IOldNewGrob , you can use explicit interface implementation like this: 编辑:如果想法是FrobFrob的成员, IOldNewGrob可以使用如下所示的显式接口实现:

public sealed class Grob : IOldNewGrob
{
    private static readonly Grob instance = new Grob();

    public static Grob Instance { get { return instance; } }

    // Prevent external instantiation
    private Grob() {}

    public static void Frob()
    {
        // Implementation
    }

    // Explicit interface implementation to prevent name collisions
    void IOldNewGrob.Frob()
    {
        // Call the static implementation
        Frob();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM