Ejemplo n.º 1
0
        /// <summary>
        /// 自动管理对象池例子
        /// </summary>
        private void ActivatorPoolExample()
        {
            Log.L("");
            Log.L("————这个例子讲述如何使用自动管理的对象池————");
            Log.L("创建一个自动管理的存放Obj_A的对象池");
            ActivatorCreatePool <Obj_A> pool = new ActivatorCreatePool <Obj_A>();

            Log.L("用对象池的Get方法获取一个实例");
            Log.L($"当前池中的对象数量为{pool.count}");
            var _obj = pool.Get();

            Log.L($"拿出的实例类型为 {_obj.GetType()}");
            Log.L("用对象池的Set方法将实例放回对象池");
            pool.Set(_obj);
            Log.L($"当前池中的对象数量为{pool.count} \n");


            Log.L("可以带参数创建一个自动管理的对象池");
            Log.L("这个参数会作为构造函数的参数使用");
            Log.L("传入年龄和姓名创建一个管理Human对象的对象池");
            ActivatorCreatePool <Human> pool_c = new ActivatorCreatePool <Human>(33, "吉良吉影");
            var human = pool_c.Get();

            Log.L("调用对象的Say方法:");
            human.Say();
        }
Ejemplo n.º 2
0
        private void FastExample()
        {
            Log.L("Create a auto create  pool");
            ActivatorCreatePool <Obj_A> pool = new ActivatorCreatePool <Obj_A>();

            Log.L("Get an instance from  pool");

            var _obj = pool.Get();

            Log.L($"the type of instance is {_obj.GetType()}");
            Log.L("Let's put the instance to pool");
            pool.Set(_obj);
            ActivatorCreatePool <Obj_C> pool_c = new ActivatorCreatePool <Obj_C>(15);

            pool_c.Get();
        }