C#.Net - 如何只创建该类的一个对象,并在整个项目中使用相同的对象

时间:2022-11-25 00:28:10

I am going to create windows form application.

我打算创建Windows窗体应用程序。

In that, I want to create only one object of Commport class and make it globalize. So that I can use all the member of that class through out my project. I saw questions like this on stack. Every body suggested to use singleton pattern. I tried to create CommPort class as singleton but I am unable to understand, how to create its object or in which class or exactly where to create. And how to call its function. Any help will be appreciated.

在那里,我想只创建一个Commport类的对象并使其全局化。这样我就可以在我的项目中使用该类的所有成员。我在堆栈上看到了这样的问题。每个人都建议使用单身模式。我试图创建CommPort类作为单例,但我无法理解,如何创建其对象或在哪个类或确切的创建位置。以及如何调用它的功能。任何帮助将不胜感激。

My application contains following class.

我的应用程序包含以下类。

main form:

主要形式:

namespace Testcertificate
{
    public partial class Testcertificate : Form
    {
        public Testcertificate()
        {
            InitializeComponent();
        }

        private void bt_Connect_Click(object sender, EventArgs e)
        {
            Connect connect = new Connect();
            connect.ShowDialog();
        }
    }
}

Second Form:

第二表:

namespace Testcertificate
{
    public partial class Connect : Form
    {
        public Connect()
        {
            InitializeComponent();
        }
    }
}

I need to create two more classes

我需要再创建两个类

one class:

一节课:

namespace Testcertificate
{
    class TxBuffer
    {
    }
}

And another one is:

另一个是:

namespace Testcertificate
{
    class CommPort
    {
    }
}

2 个解决方案

#1


2  

Did you see this explanation? It’s clearly. Singleton pattern explanation by microsoft Probably it could help you.

你看到这个解释了吗?很清楚。微软的Singleton模式解释可能它可以帮助你。

Summary You may use one of this variants of implementation:

总结您可以使用以下实现变体之一:

  1. “Classic” Singleton;
  2. “经典”单身人士;
  3. Static.
  4. 静态的。

Classic Singleton is:

经典单身人士是:

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

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

And static is:

而静态是:

public sealed class Singleton
{
   private static readonly Singleton instance = new Singleton();

   private Singleton(){}

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

And there is how to create instance of:

还有如何创建实例:

class Program
{
    static void Main(string[] args)
    {
        Computer comp = new Computer();
        comp.Launch("Windows 8.1");
        Console.WriteLine(comp.OS.Name);
         
// you can’t edit OS because object already created
        comp.OS = OS.getInstance("Windows 10");
        Console.WriteLine(comp.OS.Name);
         
        Console.ReadLine();
    }
}
class Computer
{
    public OS OS { get; set; }
    public void Launch(string osName)
    {
        OS = OS.getInstance(osName);
    }
}
class OS
{
    private static OS instance;
 
    public string Name { get; private set; }
 
    protected OS(string name)
    {
        this.Name=name;
    }
 
    public static OS getInstance(string name)
    {
        if (instance == null)
            instance = new OS(name);
         return instance;
}
}

#2


1  

Then you have to create a singleton class like this

然后你必须像这样创建一个单例类

public sealed class Singleton
{
    Singleton()
    {
    }
    private static readonly object padlock = new object();
    private static Singleton instance = null;
    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

#1


2  

Did you see this explanation? It’s clearly. Singleton pattern explanation by microsoft Probably it could help you.

你看到这个解释了吗?很清楚。微软的Singleton模式解释可能它可以帮助你。

Summary You may use one of this variants of implementation:

总结您可以使用以下实现变体之一:

  1. “Classic” Singleton;
  2. “经典”单身人士;
  3. Static.
  4. 静态的。

Classic Singleton is:

经典单身人士是:

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

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

And static is:

而静态是:

public sealed class Singleton
{
   private static readonly Singleton instance = new Singleton();

   private Singleton(){}

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

And there is how to create instance of:

还有如何创建实例:

class Program
{
    static void Main(string[] args)
    {
        Computer comp = new Computer();
        comp.Launch("Windows 8.1");
        Console.WriteLine(comp.OS.Name);
         
// you can’t edit OS because object already created
        comp.OS = OS.getInstance("Windows 10");
        Console.WriteLine(comp.OS.Name);
         
        Console.ReadLine();
    }
}
class Computer
{
    public OS OS { get; set; }
    public void Launch(string osName)
    {
        OS = OS.getInstance(osName);
    }
}
class OS
{
    private static OS instance;
 
    public string Name { get; private set; }
 
    protected OS(string name)
    {
        this.Name=name;
    }
 
    public static OS getInstance(string name)
    {
        if (instance == null)
            instance = new OS(name);
         return instance;
}
}

#2


1  

Then you have to create a singleton class like this

然后你必须像这样创建一个单例类

public sealed class Singleton
{
    Singleton()
    {
    }
    private static readonly object padlock = new object();
    private static Singleton instance = null;
    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}