单例模式介绍
确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。
参数详解
- Singleton 单例 (LoadBalancer 负载均衡器)
定义了一个Instance操作,让客户端访问它的唯一实例。 Instance是一个类操作。
*负责创建和维护自己的独特实例。
单例模式
//-------------------------------------------------------------------------------------
// SingletonStructure.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SingletonStructure : MonoBehaviour
{
void Start()
{
// Constructor is protected -- cannot use new
Singleton s1 = Singleton.Instance();
Singleton s2 = Singleton.Instance();
// Test for same instance
if (s1 == s2)
{
Debug.Log("Objects are the same instance");
}
}
}
/// <summary>
/// The 'Singleton' class
/// </summary>
class Singleton
{
private static Singleton _instance;
// Constructor is 'protected'
protected Singleton()
{
}
public static Singleton Instance()
{
// Uses lazy initialization.
// Note: this is not thread safe.
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
单例模式案例1
//-------------------------------------------------------------------------------------
// SingletonPatternExample1.cs
//-------------------------------------------------------------------------------------
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//这个真实世界的代码演示了Singleton模式作为一个负载均衡对象。 只能创建该类的单个实例(singleton)
//因为服务器可能动态地进入或离线,每个请求必须通过一个对象,该对象拥有(web)场的状态信息。
namespace SingletonPatternExample1
{
public class SingletonPatternExample1 : MonoBehaviour
{
void Start()
{
LoadBalancer b1 = LoadBalancer.GetLoadBalancer();
LoadBalancer b2 = LoadBalancer.GetLoadBalancer();
LoadBalancer b3 = LoadBalancer.GetLoadBalancer();
LoadBalancer b4 = LoadBalancer.GetLoadBalancer();
// Same instance?
if (b1 == b2 && b2 == b3 && b3 == b4)
{
Debug.Log("Same instance\n");
}
// 负载平衡15个服务器请求
LoadBalancer balancer = LoadBalancer.GetLoadBalancer();
for (int i = 0; i < 15; i++)
{
string server = balancer.Server;
Debug.Log("Dispatch Request to: " + server);
}
}
}
/// <summary>
/// The 'Singleton' class
/// </summary>
class LoadBalancer
{
private static LoadBalancer _instance;
private List<string> _servers = new List<string>();
private System.Random _random = new System.Random();
// 锁定同步对象
private static object syncLock = new object();
// Constructor (protected)
protected LoadBalancer()
{
// List of available servers
_servers.Add("ServerI");
_servers.Add("ServerII");
_servers.Add("ServerIII");
_servers.Add("ServerIV");
_servers.Add("ServerV");
}
public static LoadBalancer GetLoadBalancer()
{
//支持多线程应用程序通过'双重检查锁定'模式
//(一旦实例存在)避免每次调用该方法时锁定
if (_instance == null)
{
lock (syncLock)
{
if (_instance == null)
{
_instance = new LoadBalancer();
}
}
}
return _instance;
}
// 简单,但有效的随机负载均衡器
public string Server
{
get
{
int r = _random.Next(_servers.Count);
return _servers[r].ToString();
}
}
}
}
单例模式案例2
//-------------------------------------------------------------------------------------
// SingletonPatternExample2.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
namespace SingletonPatternExample2
{
public class SingletonPatternExample2 : MonoBehaviour
{
void Start()
{
RenderManager.Instance.Show();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
RenderManager.Instance.Show();
}
}
}
/// <summary>
/// 某单例manager
/// </summary>
public class RenderManager
{
private static RenderManager instance;
public static RenderManager Instance
{
get
{
if (instance == null)
{
instance = new RenderManager();
}
return instance;
}
}
/// <summary>
/// 随便某方法
/// </summary>
public void Show()
{
Debug.Log("RenderManager is a Singleton !");
}
}
}
单例模式案例3
//-------------------------------------------------------------------------------------
// SingletonPatternExample3.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
namespace SingletonPatternExample3
{
public class SingletonPatternExample3 : MonoBehaviour
{
void Start()
{
//单例A
SingletonA.Instance.DoSomething();
//单例B
SingletonB.Instance.DoSomething();
}
}
/// <summary>
/// 单例类基类(抽象类、泛型,其他类只需继承此类即可成为单例类)
/// 继承该类的,即成为一个单例类
/// </summary>
public abstract class Singleton<T>
where T : class, new()
{
private static T _instance = null;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = new T();
return _instance;
}
return _instance;
}
}
protected virtual void Awake()
{
_instance = this as T;
}
}
/// <summary>
/// 继承自Singleton<T>的单例
/// </summary>
public class SingletonA : Singleton<SingletonA>
{
public void DoSomething()
{
Debug.Log("SingletonA:DoSomething!");
}
}
/// <summary>
/// 继承自Singleton<T>的单例
/// </summary>
public class SingletonB : Singleton<SingletonB>
{
public void DoSomething()
{
Debug.Log("SingletonB:DoSomething!");
}
}
}