您的位置:首页 > 编程语言 > C#

C# 单例模式和泛型应用

2015-08-24 14:56 543 查看

C# 单例模式和泛型应用

在游戏开发中难免会有很多公用的类,我们可能要在很多类中,得到这些公用类的引用,我们很容易想到单例模式

单例模式

单例模式需要在类中定义该类的一个私有的静态变量,和一个获取改静态变量的公有的静态方法,使用的时候,通过类名调用改静态方法即可

using System;
using System.Collections;
using System.Collections.Generic;

namespace mySingleTon
{
public class Singleton
{
private static Singleton instance;

private Singleton()
{

}

public static Singleton GetInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
public void testFun()
{
Console.WriteLine("in method testFun");
}
}
}


多线程下的单例

using System;
using System.Collections;
using System.Collections.Generic;

namespace mySingleTon
{

public class Singleton
{
// 单例模式的实现
// 定义一个静态变量来保存类的实例
private static Singleton uniqueInstance;

// 定义一个标识确保线程同步
private static readonly object locker = new object();

// 定义私有构造函数,使外界不能创建该类实例
private Singleton()
{
}

/// <summary>
/// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
/// </summary>
/// <returns></returns>
public static Singleton GetInstance()
{

// 当第一个线程运行到这里时,此时会对locker对象 "加锁",

// 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁

// lock语句运行完之后(即线程运行完之后)会对该对象"解锁"

// 双重锁定只需要一句判断就可以了
if (uniqueInstance == null)
{
lock (locker)
{

// 如果类的实例不存在则创建,否则直接返回
if (uniqueInstance == null)
{
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
//testFun
public void testFun()
{
Console.Write("in method testFun");
}
}
}


使用:
Singleton.GetInstance().testFun();


一个公用类,我们想到了使用单例,那么如果有很多个公用的类呢 ?在游戏开发中,难免会在许多类中调用其他各种各样的类的方法,这时候,泛型闪亮登场了

泛型

首先了解什么是泛型(这个自行百度,google - -)
接下来就到正题了,我们想办法将这些个公共类存到一个List(表示可通过索引访问的对象的强类型列表。 提供用于对列表进行搜索、排序和操作的方法)里面,。

Service.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyService
{
public static class Service
{
private static List<IServiceWrapper> serviceWrapperList;

public static void Set<T>(T instance)
{
if (ServiceWrapper<T>.instance != null)
{
throw new Exception("An instance of this service class has already been set!");
}

ServiceWrapper<T>.instance = instance;

if (serviceWrapperList == null)
{
serviceWrapperList = new List<IServiceWrapper>();
}

serviceWrapperList.Add(new ServiceWrapper<T>());
}

public static T Get<T>()
{
return ServiceWrapper<T>.instance;
}

public static bool IsSet<T>()
{
return ServiceWrapper<T>.instance != null;
}

public static void ResetAll()
{
if (serviceWrapperList == null)
{
return;
}

// Unset in the reverse order in which services were set.  Probably doesn't matter.
for (int i = serviceWrapperList.Count - 1; i >= 0; i--)
{
serviceWrapperList[i].Unset();
}

serviceWrapperList = null;
}
}

internal class ServiceWrapper<T> : IServiceWrapper
{

public static T instance = default(T);

public void Unset()
{
ServiceWrapper<T>.instance = default(T);
}
}

internal interface IServiceWrapper
{
void Unset();
}
}


假如我们现在有一个用于管理声音的类AudioController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyService;
namespace MyAudioController
{
class AudioController
{
public AudioController()
{
Service.Set<AudioController>(this);
}
public void SetVolume(float volume)
{
Console.WriteLine("volue = {0}",volume);
}
}
}


主要看构造方法,在构造方法中,调用了Service.Set<T>(T instance),这里说明,只要创建一个AudioManager的实例,这个实例就会被添加到Service类中的serviceWrapperList列表中,这个列表用于保存所有要用到个公共类的引用.

使用:
Service.Get<AudioController>().SetVolume(1.0f);


如果有其他的公共类,只需要在构造方法中,调用Service类的Set<T>(T instance)方法即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: