这个单例有办法可以改进吗?



我使用的是google的singleton,但这肯定需要太多的引用。

的例子,当我必须使用另一个类在我的播放器类使用单例,我必须使用引用三次。如下:Player.instance.another.blank=0;

我的单

public static Player instance;

public void Awake()

{

if(instance ==null){

instance=this;

}

else

{

if(instance!=this){

Destroy(this.gameObject);

}

}

是否有理由销毁实例?即使如此,我们也不会在销毁现有的实例后立即更新它。

我有一个我通常使用的单例Gist: https://gist.github.com/xepherys/34d3d5ce3f44749e8649a25b38127347

对于不熟悉单例的人来说,它有不错的注释,并且是线程安全的。您可以删除除lazy字段和构造函数区域之外的所有内容。我用它作为Manager类的基础。

using System;
// Update namespace as needed
namespace WhatsYourName
{
/*
This is the name of your threadsafe Singleton - change "SingletonLazyThreadsafe" to value that makes sense, and be sure to use your
editors [Rename] option, or update all values to match.
Just because the Singleton itself is threadsafe does not mean that all methods that might be contained are automatically threadsafe.
If threading is important, use threadsafe variables, such as:

System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue>
https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2
rather than:
System.Collections.Generic.Dictionary<TKey,TValue>
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2
Alternatively, lock() can be used in a pinch, but there is the potential for slight performance hits.

Any field, property, or method not marked with "// REQUIRED" means that it's just a sample and can be removed or changed as needed.
Comments are inline as a reminder and as a point of education for those not familiar with Singletons.
Initial snippet added 12/08/2018 - JSW (Xepherys).
*/
public class SingletonLazyThreadsafe
{
#region Fields
// Private
private static readonly Lazy<SingletonLazyThreadsafe> lazy = new Lazy<SingletonLazyThreadsafe>(() => new SingletonLazyThreadsafe());  // REQUIRED
private int changeCount;
private int myInteger;
private string myString;
// Public
public char MyPublicChar;  // Note: Even though it's a field, if it's publicly accessible, I generally capitalize the first letter.  This is a personal design choice.  Most folk tend to use lowercase for fields regardless of their accessibility level.
#endregion
#region Properties
// Note: Private getter/setter for private field.
private int ChangeCount
{
get
{
return this.changeCount;
}
set
{
this.changeCount = value;
}
}
// Note: Public getter/setter for private field.
public int MyInteger
{
get
{
return this.myInteger;
}
set
{
this.myInteger = value;
}
}
// Note: Public getter / protected setter for private field.  This allows a {get} from anywhere, but only a {set} from inside the class or derived classes.
public string MyString
{
get
{
return this.myString;
}
protected set
{
this.myString = value;
}
}
#endregion
#region Constructors
private SingletonLazyThreadsafe()  // REQUIRED
{ }
public static SingletonLazyThreadsafe Instance  // REQUIRED
{
get
{
return lazy.Value;
}
}
#endregion
#region Methods
// Note: This is a public method that just changes the myInteger field.  It's useless since the property is public, but it's just an example.  It also call IncreaseCount().
public void IncrementInteger(int value)
{
this.MyInteger = value;
IncreaseCount();
}
// Note: This is a public method that just changes the myString field.  It's useless since the property is public, but it's just an example.  It also call IncreaseCount().
public void ChangeString(string value)
{
this.MyString = value;
IncreaseCount();
}
// Note: This is a private method, which means it can only be called by other methods in this class, and not publicly or outside of the class.  While it could directly change
//       'changeCount', I also have it making changes via the private 'ChangeCount' property, which is also only accessible inside the class.
private void IncreaseCount()
{
this.ChangeCount++;
}
#endregion
}
}

相关内容

  • 没有找到相关文章

最新更新