PHP design mode of Service Locator mode example detailed

Source: Internet
Author: User
Service Locator He knows how to locate (create or acquire) the services that an application needs, and the service consumer does not need to care about the actual implementation of the service in real life. This article is mainly to share with you the PHP design mode of Service locator mode examples of detailed, I hope to help everyone.

What's the effect?

Decoupling service users and services without changing the code and simply configuring it to serve the service implementation.

UML diagram

code example

Class Servicelocator {/** * Service instance Index */privite $_services = [];        /** * Service Definition Index */private $_definitions = [];        /** * Whether global service sharing (Singleton mode) */Private $_shared = [];    Public function has ($id) {return isset ($this->_services[$id]) | | isset ($this->_definitions[$id]);        The Public Function __get ($id) {if ($this->has ($this->id)) {$this->get ($id); }//Another implement} public function get ($id) {if (Isset ($this->_services[$id]) &amp        ;& $this->_shared[$id]) {return $this->_services[$id]; } if (Isset ($this->_definitions[$id])) {//instantiation $definition = $this->_definitio            ns[$id]; $object = Creator::createobject ($definition);//Omit Service instantiation implementation if ($this->_shared[$id]) {$this->_        services[$id] = $object} return $object;          }      throw new Exception ("Unable to locate service {$id}")} Public function set ($id, $definition, $share = False) {if ($            definition = = = null) {unset ($this->_services[$id], $this->_definitions[$id]);        Return        } unset ($this->_services[$id]);        $this->_shared[$id] = $share;        if (is_string ($definition)) {return $this->_definitions[$id] = $definition; } if (Is_object ($definition) | | is_callable ($definition, True)) {return $this->_definitions[$id] = $        Definition } if (Is_array ($definition)) {if (Isset ($definition [' class ')]) {return $this-&G            t;_definitions[$id] = $definition;    }} throw new Exception ("service add failed"); }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.