Design pattern — object pool pattern

Time:2021-5-30

Today, let’s introduce the object pool pattern. This design pattern is not used as frequently as factory pattern in daily work, but it provides a new idea of object use and management, which is worth learning.

Since the pool is mentioned, it is a collection of a certain class or a kind of things, such as thread pool, which involves the process of putting objects into the pool and getting objects from the pool.

Let’s define an art class first

class Art
{
    private $_name;
    
    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->_name;
    }
    
    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->_name = $name;
    }
}

We also need to define an artpool to handle the in pool and out pool operations of art

class ArtPool
{
    //Art category list
    static private $_arts = [];
    
    /**
     *Adding objects to the object pool
     *
     * @param Art $art
     */
    static public function pushArt(Art $art)
    {
        if(!isset(static::$_arts[$art->getName()]) || empty(static::$_arts[$art->getName()]))
        {
            static::$_arts[$art->getName()] = $art;
        }
    }
    
    /**
     *Get object
     *
     * @param $name
     * @return mixed|null
     */
    public static function getArt($name)
    {
        return isset(self::$_arts[$name]) ? self::$_arts[$name] : null;
    }
    
    /**
     *Remove objects from the object pool
     *
     * @param $name
     */
    public static function removeArt($name)
    {
        if(array_key_exists($name, self::$_arts))
        {
            unset(self::$_arts[$name]);
        }
    }
}

There is a more important point to pay attention to

//Art category list
static private $_arts = [];

This static variable stores all art objects;

Call procedure and execution result:

class Client
{
    public static function main()
    {
        $music = new Art();
        
        $music->setName('music');
        
        //Join to object pool
        ArtPool::pushArt($music);
    
        $movie = new Art();
        
        $movie->setName('movie');

        ArtPool::pushArt($movie);
    
        $music_in_pool = ArtPool::getArt('music');
        
        p('$music_in_pool',$music_in_pool);
        
        $movie_in_pool = ArtPool::getArt('movie');
        
        p('$movie_in_pool',$movie_in_pool);
    }
}

Implementation results:

*******
$music_in_pool=
object(Art)#2 (1) {
  ["_name":"Art":private]=>
  string(5) "music"
}

*******
$movie_in_pool=
object(Art)#3 (1) {
  ["_name":"Art":private]=>
  string(5) "movie"
}

We get unused art objects according to different names, which saves memory and operation time in new time. It is mainly suitable for frequent creation and destruction of objects.

As I said at the beginning, the object pool mode is relatively simple, but it provides us with an interesting way to create objects. We can also create the entire object collection when creating pool objects, which can also reduce memory fragmentation and improve memory efficiency.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]