Sharing element mode of design mode

Posted by Rithiur on Wed, 08 Dec 2021 12:05:07 +0100

What is the meta model

Sharing yuan, a shared element, what is sharing, is to share things and put them in one place for others to enjoy. However, in the program, we can temporarily understand this element as various objects and classes, and put them into an array to facilitate unified management and call. It's a bit like the registration mode and the combination mode. They both put classes into the array, but the difference is that there is no necessary connection and interdependence between the array elements in the registration mode; The composition pattern is a tree structure with hierarchical dependency and no dependency between peers, and these elements are roughly the same because they inherit from the same interface; The meta pattern is similar to the composite pattern, except that it uses a separate factory class to store instances of classes, and each element of it must be a combination of at least two classes.

Usage scenario

For example, when sending SMS, there are x third-party SMS platforms, Alibaba cloud and Tencent, m SMS templates and n recipients, a total of three objects. If you use the traditional development mode, you need to instantiate a platform class, a template class and a recipient every time you send a short message. Then you need xmn the same operations to send a short message to everyone. Obviously, the performance is very low. At this time, it is suitable for the meta sharing mode. Save the combination of platform and template into the array and get it as you use, so as to reduce the creation of classes and reduce resource consumption.

Sketch Map

advantage

Reduce the creation of classes, save system resources, improve performance, and easy to expand, in line with the single principle and opening and closing principle of design principles.

Code example

directory structure

 

Message.php SMS platform interface class

interface Message{

    /**
     * Send SMS
     * @param User $user
     * @return mixed
     */
    function send(User $user);
}

  Ali.php Ali SMS platform class

class Ali implements Message{
    public $template; //SMS template class
    public function __construct(Template $template)
    {
        //Set SMS template
        $this->template = $template;
    }

    public function send(User $user)
    {
        // TODO: Implement send() method.
        echo "<pre>";
        echo 'Send messages to Alibaba cloud'.$user->name.'send out:'.$this->template->getTemplate().PHP_EOL;
    }
}

Tx.php Tencent SMS platform class

class Tx implements Message{
    public $template;//SMS template class
    public function __construct(Template $template)
    {
        //Set SMS template
        $this->template = $template;
    }

    public function send(User $user)
    {
        // TODO: Implement send() method.
        echo 'Send messages to Alibaba cloud'.$user->name.'send out:'.$this->template->getTemplate(),PHP_EOL;
    }
}

Template.php SMS template class

class Template{
    public $template; //SMS template
    public function __construct($template)
    {
        //Set SMS template
        $this->template = $template;
    }

    /**
     * Get SMS template
     * @return mixed
     */
    function getTemplate(){
        return $this->template;
    }
}

Factory.php meta factory class

<?php
class Factory{
    public $message=[];

    /**
     * Get the SMS class instance to be used according to the key
     * @param $key
     * @param $template
     * @return mixed
     */
    function getMessage($key,$template){
        if (!isset($this->message[$key])){
            $this->message[$key] = $template;
        }

        return $this->message[$key];
    }

    //Call the send method of the platform to send short messages
    function send($user){
        foreach ($this->message as $message){
            $message->send($user);
        }
    }
}

User.php recipient

<?php
class User{
    public $name;//user name

    public function __construct($name)
    {
        //Set user name
        $this->name = $name;
    }

    /**
     * Get user name
     * @return mixed
     */
    function getName(){
        return $this->name;
    }
}

client.php call

<?php
require_once 'Message.php';
require_once 'Ali.php';
require_once 'Template.php';
require_once 'Tx.php';
require_once 'User.php';
require_once 'Factory.php';

//Initialize three SMS templates
$template1 = new Template('Template 1');
$template2 = new Template('Template 2');
$template3 = new Template('Template 3');

//Alibaba cloud platform class and template class binding
$ali1 = new Ali($template1);
$ali2 = clone $ali1;
$ali3 = clone $ali1;
$ali2->template = $template2;
$ali3->template = $template3;

//Tencent platform class and template class binding
$tx1 = new Tx($template1);
$tx2 = clone $tx1;
$tx3 = clone $tx1;
$tx2->template = $template2;
$tx3->template = $template3;

//The meta factory stores these bindings
$factory = new Factory();
$factory->getMessage(md5($template1->template.'ali'),$ali1);//Storage alicloud platform binding to template 1
$factory->getMessage(md5($template2->template.'ali'),$ali2);//Storage Alibaba cloud platform binding to template 2
$factory->getMessage(md5($template3->template.'ali'),$ali3);//Storage Alibaba cloud platform binding with template 3

$factory->getMessage(md5($template1->template.'tx'),$tx1);//Storage alicloud platform binding to template 1
$factory->getMessage(md5($template3->template.'tx'),$tx3);//Storage Alibaba cloud platform binding with template 3

//Recipient
$user1 = new User('Wang Wu');
$user2 = new User('Li Si');

//Send separately
$factory->send($user1);
$factory->send($user2);

Operation results

 

 

Topics: Design Pattern