Giter Site home page Giter Site logo

object-pool's Introduction

最近的Android项目里面需要频繁的添加/删除元素,导致了对象的频繁创建且难以管理,所以抽空实现了对象池技术,使用起来很简单。

一共就三个类,CacheObject,CacheObjectContainer,CacheObjectManager。

CacheObject用来包装真实的对象,里面有一个布尔值,代表该对象是否被使用中。

CacheObjectContainer本质上就是缓存某种类型的对象列表。

CacheObjectManager提供外部接口,所有的获取对象,释放对象,销毁对象的接口都在该类中实现。

简单介绍下使用方式:

比如:

class A{}

class B{}

我们获取A的对象时,直接使用CacheObjectManager.getDefault().getObject(A.class)即可获取到对象,不用自己创建。  程序内部会自动检索是否有空闲的对象,如果有空闲的对象就返回空闲的对象,否则就创建一个新的对象返回。

CacheObjectManager.getDefault().getObject(A.class)
    不用的时候,使用CacheObjectManager.getDefault().release(a)即可把对象还给对象池。被返回的对象就是空闲的了,供下一次使用。

CacheObjectManager.getDefault().release(a)
    确定以后不再使用时,可以使用

CacheObjectManager.getDefault().destory(A.class)
即可删除所有A类型的对象缓存。

object-pool's People

Contributors

freshman585 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.