在 .NET 中处理单例对象

发布于 2024-08-23 12:24:16 字数 830 浏览 6 评论 0原文

我有两个类 ClassA 和 ClassB 都具有对单例对象 ClassHelper 的引用。我的问题是使用 ClassA 和 ClassB 完成后我应该如何处理单例对象

编辑:

public ClassA
{
  CHelper obj;

  public ClassA()
  {
    obj = obj.GetInstance("Initialise");
    obj.CallFuncA();
  }
}

On the same lines
public ClassB
{
  CHelper obj;

  public ClassB()
  {
    obj = obj.GetInstance("Initialise");
    obj.CallFuncB();
  }
}


where 

CHelper
{
   private static sm_CHelper;

   public static GetInstance(string strInitialise)
   {
      if(sm_CHelper == null)
      {
         sm_CHelper = new CHelper(strInitialise);
      }
   }

   private CHelper(string strInitialise)
   {
      //do something here 
   }

   public CallFuncA()
   {
     // do something here
   }
   public CallFuncB()
   {
     // do something here
   }
}

问候 学习者

I have two classes ClassA and ClassB both having a reference to a singleton object ClassHelper. My question is how should i dispose the singleton object once im done using both the ClassA and ClassB

Edit:

public ClassA
{
  CHelper obj;

  public ClassA()
  {
    obj = obj.GetInstance("Initialise");
    obj.CallFuncA();
  }
}

On the same lines
public ClassB
{
  CHelper obj;

  public ClassB()
  {
    obj = obj.GetInstance("Initialise");
    obj.CallFuncB();
  }
}


where 

CHelper
{
   private static sm_CHelper;

   public static GetInstance(string strInitialise)
   {
      if(sm_CHelper == null)
      {
         sm_CHelper = new CHelper(strInitialise);
      }
   }

   private CHelper(string strInitialise)
   {
      //do something here 
   }

   public CallFuncA()
   {
     // do something here
   }
   public CallFuncB()
   {
     // do something here
   }
}

Regards
Learner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心意如水 2024-08-30 12:24:16

如果您正在谈论模式 singleton,那么您不应该处置它......如果您没有提到 singleton 模式,那么您可以尝试使用解构函数来运行您的处置逻辑。

if you are talking about the pattern singelton then you should not dispose it.... if your not referring to the singelton pattern then you could try to use the deconstructor to run your dispose logic.

与君绝 2024-08-30 12:24:16

该单例应该在应用程序期间保持活动状态。因此,在处理 A 类和 B 类产品时,不应将其丢弃。

That singleton should remain alive for the duration of the application. So you shouldn't dispose of it when disposing of those ClassA and ClassB's.

网名女生简单气质 2024-08-30 12:24:16

我从未见过这样的例子。我可能会这样做:

 class Resource {

     static Resource Instance  = new Resource();
     static int count = 2;

     public Resource CheckOut() { 
        if (--count <= 0)
            Instance = null;
        return Instance;
     }
 }

这样,在 ClassA 和 ClassB 检出资源后,静态引用将停止保留它。在 ClassA 和 ClassB 失去对资源的引用后,终结器将在下一轮垃圾收集中被调用。

I've never seen an example like this. I'd probably do something like:

 class Resource {

     static Resource Instance  = new Resource();
     static int count = 2;

     public Resource CheckOut() { 
        if (--count <= 0)
            Instance = null;
        return Instance;
     }
 }

this way, after both ClassA and ClassB checkout the Resource, the static reference stops keeping it around. After ClassA and ClassB lose their reference to the Resource, the finalizer gets called the next round of garbage collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文