Detailed explanation of service locator pattern examples of PHP design patterns

小云云
Release: 2023-03-19 16:00:01
Original
1878 people have browsed it

Service locator (service locator) knows how to locate (create or obtain) the services required by an application. Service users do not need to care about the actual implementation of the service in actual use. This article mainly shares with you a detailed explanation of the service locator pattern example of PHP design pattern. I hope it can help you.

What is the role

To achieve the decoupling of service users and services, there is no need to change the code but only by simply configuring and updating the service.

UML diagram
Detailed explanation of service locator pattern examples of PHP design patterns

##Code sample

class ServiceLocator {

    /**
     * 服务实例索引
     */
    privite $_services = [];

    /**
     * 服务定义索引
     */
    private $_definitions = [];
    
    /**
     * 是否全局服务共享(单例模式)
     */
    private $_shared = [];
    
    public function has($id){
        return isset($this->_services[$id]) || isset($this->_definitions[$id]);
    }
    
    public function __get($id){
        if($this->has($this->id)){
            $this->get($id);
        }
        
        // another implement
    }
    
    public function get($id){
        if(isset($this->_services[$id]) && $this->_shared[$id]){
            return $this->_services[$id];
        }
        
        if (isset($this->_definitions[$id])) {
            // 实例化
            $definition = $this->_definitions[$id];
            $object = Creator::createObject($definition);//省略服务实例化实现
            if($this->_shared[$id]){
                $this->_services[$id] = $object
            }
            
            return $object;
        }
        
        throw new Exception("无法定位服务{$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->_definitions[$id] = $definition;
            }
        }
        
        throw new Exception("服务添加失败");
    }
}
Copy after login
Related recommendations :


Detailed explanation of the delegation pattern of PHP design patterns

Detailed explanation of the memo pattern of PHP design patterns

Detailed explanation of PHP design pattern builder pattern


The above is the detailed content of Detailed explanation of service locator pattern examples of PHP design patterns. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template