使用篇八,对象池
2021/7/15 6:08:07
本文主要是介绍使用篇八,对象池,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在开头
相信看到对象池,大家是很熟悉了 就算你不熟悉,如果你在CSDN 搜索的话,我相信可以搜索到 一堆开手枪的 unity版本对象池 那么IF的有什么区别?IF主要提供了三种对象池 1、自动实例化的对象池 2、自己个性化管理生命周期的对象池 3、基类对象池,这是啥?简单讲解:声明一个动物的对象池,你可以取出猫,同时也可以取出狗
上马,上马
public class PoolTest : Test { public interface IObject { } public class Obj_A : IObject { } public class Obj_B : IObject { } 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); } private class MyPool : ObjectPool<Obj_A> { protected override Obj_A CreatNew(IEventArgs arg) { return new Obj_A(); } } private void NormalTest() { Log.L("Create a auto create pool"); MyPool pool = new MyPool(); 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); } private class MutiPool : BaseTypePool<IObject> { } private void MutiTest() { Log.L("Create a auto create pool"); MutiPool pool = new MutiPool(); Log.L("Get an instance A from pool"); IObject _obj = pool.Get<Obj_A>(); Log.L($"the type of instance is {_obj.GetType()}"); Log.L("Let's put the instance to pool"); pool.Set(_obj); Log.L("Get an instance B from pool"); _obj = pool.Get<Obj_B>(); Log.L($"the type of instance is {_obj.GetType()}"); Log.L("Let's put the instance to pool"); pool.Set(_obj); } protected override void Start() { Log.L("--------------------------"); FastExample(); Log.L("--------------------------"); NormalTest(); Log.L("--------------------------"); MutiTest(); } protected override void Stop() { } protected override void Update() { } }
让我们来看看结果,用起来很简单吧
这篇关于使用篇八,对象池的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现