單例模式(Singleton)在GOF 中的說明是:
「確認類別只有一個物件,並且提供一個全域的方法來取得這個物件」
講得更簡單點,Singleton的精神就是下面兩點
因為其的特性讓它在Unity裡擁有只要拉一個物件就能直接運作的能力,如果翻過很多Unity工具的話應該會蠻常看到工具本身的主程式是用 static + Instance這個組合來方便快速獲取常用的Function。
TurnBaseStrategySystem.cs
using LinXuan.TBSF.TurnTtates;
using LinXuan.TBSF.Units;
using System.Collections.Generic;
namespace LinXuan.TBSF
{
public class TurnBaseStrategySystem
{
private static TurnBaseStrategySystem m_Instance;
public static TurnBaseStrategySystem Instance
{
get
{
if (m_Instance == null)
m_Instance = new TurnBaseStrategySystem();
return m_Instance;
}
}
private TurnBaseStrategySystem()
{
}
private ITurnState m_CurrentTurnState;
public ITurnState TurnState
{
get => m_CurrentTurnState;
set
{
ITurnState nexeTurnState;
}
}
public List<IUnit> Units { get; private set; }
/// <summary>
/// TODO:If game finished, destory turn base strategy system instance.
/// </summary>
public bool GameFinished { get; private set; }
public void Initinalize()
{
}
public void StartGame()
{
}
}
}
設計模式與遊戲開發的完美結合(暢銷回饋版)
Turn Based Strategy Framework
流離之歌