예제 #1
0
        static void MakeBookings(SingletonType singletonType, string channel)
        {
            NumberGenerator counter = null;

            switch (singletonType)
            {
            case SingletonType.NotThreadSafe:
                counter = SingletonV1NotThreadSafe.CounterInstance;
                break;

            case SingletonType.SimpleThreadSafe:
                counter = SingletonV2SimpleThreadSafe.CounterInstance;
                break;

            case SingletonType.ThreadSafeWithoutLocks:
                counter = SingletonV3ThreadSafeWithoutLocks.CounterInstance;
                break;

            case SingletonType.FullyLazyInstantiation:
                counter = SingletonV4FullyLazyInstantiation.CounterInstance;
                break;

            case SingletonType.Lazy:
                counter = SingletonV5Lazy.CounterInstance;
                break;
            }

            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine($"Booking on {channel} with {singletonType} singleton: {counter.GetNumber()}");
            }
        }
예제 #2
0
        // initializaion singleton
        public virtual void Initialize(SingletonType type = SingletonType.DestroyOnLoad)
        {
            if (Instance == null)
            {
                Instance = this.GetComponent <T>();
                Log.Trace("[ Singleton - Instantiated ] " + typeof(T).ToString());
            }

            if (type == SingletonType.DontDestroyOnLoad)
            {
                m_SingletonType = type;
                DontDestroyOnLoad(this.gameObject);
            }
        }
예제 #3
0
 /// <summary>
 /// 检测场景中是否只存在一个
 /// </summary>
 void Main()
 {
     singletonState = CheckSingleton();
     if (singletonState != SingletonType.Complete)
     {
         return;
     }
     UnityEngine.Object[] objs = GameObject.FindObjectsOfType(this.GetType());
     if (objs.Length > 1)
     {
         instance    = null;
         isSingleton = false;
     }
 }
예제 #4
0
        private void simulate(SingletonType singletonType)
        {
            for (int i = 0; i < 10; i++)
            {
                Task.Run(() =>
                {
                    for (int j = 0; j < 30; j++)
                    {
                        // var amt = this.getRandomAmt(); //預購數
                        var amt = 2; //預購數

                        NumberProvider counter = null;

                        switch (singletonType)
                        {
                        case SingletonType.NonThreadSafe:
                            counter = NonThreadSafeSingleton.Instance;
                            break;

                        case SingletonType.DoubleCheck:
                            counter = DoubleCheckSingleton.Instance;
                            break;

                        case SingletonType.Eager:
                            counter = EagerSingleton.Instance;
                            break;

                        case SingletonType.Lazy:
                            counter = LazySingleton.Instance;
                            break;
                        }

                        string preorderNumbers = string.Empty; //預購序號集合
                        for (int x = 0; x < amt; x++)
                        {
                            var number       = counter.GetNumber();
                            preorderNumbers += $"*{number}* ";
                        }

                        Trace.WriteLine($"({Thread.CurrentThread.ManagedThreadId}){this.getRandomName()}預購{amt}組: {preorderNumbers}");
                    }
                });
            }
        }
예제 #5
0
    //Usable Functions
    /// <summary>
    /// Register a Manager as singleton
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="manager"></param>
    public static void Register <T> (T manager, SingletonType type) where T : MonoBehaviour
    {
        SingletonInfo singletonInfo = new SingletonInfo(manager, type);

        if (InstancesList.Count == 0)
        {
            InstancesList.Add(singletonInfo);
        }
        else
        {
            if (InstancesList.Contains(singletonInfo) == false)
            {
                //Debug.Log("Added " + manager + " as Singleton.");
                InstancesList.Add(singletonInfo);
            }
            else
            {
                Debug.Log(manager + " Manager Already Existed. Ignored.");
            }
        }
    }
예제 #6
0
 public virtual void Deserialize(BinaryReader reader)
 {
     tid           = new TID();
     tid.id        = reader.ReadInt32();
     singletonType = (SingletonType)reader.ReadByte();
 }
예제 #7
0
 public SingletonInfo(MonoBehaviour instance, SingletonType type)
 {
     Instance = instance;
     Type     = type;
 }