Design pattern — multi case pattern

Time:2021-9-4

In the last article, we talked about the single case mode. Some children’s shoes will ask: there are single cases, are there multiple cases? Answer: Yes, in this article, let’s talk about multi instance mode.

Multi instance mode means that a class has multiple instances, and the instances are the class itself. Such a class is called a multi instance class and has the following characteristics:

  • A multi instance class can have multiple instances.
  • Multi instance classes must create and manage their own instances, and provide their own instances to the outside world.

In a word, unlike ordinary new objects, we can create new objects many times, but each new object is a new object; All instances in a multi instance class are the class itself.

Let’s take a look at the specific code implementation:
Logic: the CD factory should not authorize different content to different factories for production: let Factory 1 produce the content of CD1; The content of CD2 allows factory 2 to produce. This can reduce the number of CD factories and increase the scalability of CD factories.

class CDFactory
{
    /**
     *Instance object array
     *
     * @var array
     */
    private static $_instances = [];
    
    /**
     *The constructor is private to prevent the class from being instantiated through new
     *
     * @access private
     * @return void
     */
    private function __construct ()
    {
    
    }
    
    /**
     *Privatize replication and cloning methods to prevent category replication and cloning
     *
     * @access private
     * @return void
     */
    private function __clone ()
    {
    
    }
    
    /**
     *Prevent instances from being serialized
     *
     * @access private
     * @return void
     */
    private function __wakeup ()
    {
    }
    
    /**
     *Define the entry to get the object instance and return the instance
     *
     * @access public
     *@ param string $name instantiation name
     * @return self
     */
    public static function getInstance (string $name)
    {
        //Determine whether an instantiated object already exists
        if (!isset(self::$_instances[$name]))
        {
            p('new intance:' . __CLASS__);
            
            //If it does not exist, instantiate one
            self::$_instances[$name] = new self();
        }
        
        return self::$_instances[$name];
    }
    
    /**
     *This interface is used to test singleton mode - change the value of string
     *
     * @access public
     *@ param string $string value
     * @return void
     */
    public function setContent($string)
    {
        $this->string = $string;
    }
    
    /**
     *This interface is used to test singleton mode - output the value of string
     *
     * @access public
     * @return void
     */
    public function getContent()
    {
        p('$this->string',$this->string);
    }
}

Code called:

class Client
{
    static public function factory1Produce()
    {
        $cd = CDFactory::getInstance('CD1');
    
        $CD - > setcontent ('cd 1 content ~ ');
        
        return $cd;
    }
    
    static public function factory2Produce()
    {
        $cd = CDFactory::getInstance('CD2');
        
        $CD - > setcontent ('cd 2 content ~ ');
        
        return $cd;
    }
    
    /**
     *Main function
     */
    static public function main()
    {
        //Plant 1 production
        $cd_1 = static::factory1Produce();
        
        $cd_1->getContent();
        
        //Plant 2 production
        $cd_2 = static::factory2Produce();
        
        $cd_2->getContent();
    }
}

Execution results:

new intance:CDFactory
$this->string=
String (12) "CD 1 content ~"
new intance:CDFactory
$this->string=
String (12) "CD 2 content ~"

In the execution result, we can see that new intance: cdfactory is called twice to implement a class with multiple instances, because our instances exist$_ Instances array, so every new entry is the CD class itself.

In practical application, multi instance mode can be used to deal with similar functions such as multiple database connectors, but different instances.

Multi instance mode and single instance mode are relatively simple design modes, and they are also frequently used design modes, especially the single instance mode. For example, the cache in ThinkPHP uses the multi instance mode, but the redis connection processing uses the single instance mode.