Home  >  Article  >  Backend Development  >  About registry mode

About registry mode

WBOY
WBOYOriginal
2016-08-08 09:32:451489browse
Usually applications can be divided into the following levels:
1. Front-end view
2. Command control
3.Business logic
4.Data processing
The front-end view is responsible for displaying user data and collecting user input data, and submitting it to the command and control layer. The command control layer receives the data and performs a series of processing, and entrusts the business logic layer to complete specific tasks. The business logic layer calls the data processing module to complete the storage of user data.
But how can the data submitted by the front-end be directly and appropriately transmitted at several levels? One is to pass it through a context class provided by the previous command mode. Place the parameters in the context object and pass them from the command control layer to the business logic layer. After completing a series of operations, the operation results are returned through the context. The second way is to modify the interface of the command object to adapt to the corresponding data transfer. These two methods sometimes break encapsulation. We know that the singleton pattern provides another way to access global variables. Static variables are locally hidden and can be easily set and obtained through interfaces. The registration mode (Registry) takes advantage of this convenience.

The registration mode can also be regarded as a singleton version of the context object.

A simple Registry implementation:

	abstract class Registry{
		abstract protected function get($key);
		abstract protected function set($key, $value);
	}
PHP supports three types of object data life cycles: one starts from receiving an HTTP request and ends after the request is processed. The other is to support session level objects, that is, the object data can be stored in the session. PHP is in
During session_start, different object data are restored based on the session ID stored in cookies, which can achieve the purpose of the same user requesting access to the same object data multiple times. There is also an application scope level. i.e.
of the same application Different users can share the same object data. This operation requires PHP's built-in serialization function to complete.


Data registration mode based on one HTTP request:

class RequestRegistry extends Registry{
		private static $instance;
		private $values = array();
		
		private function __construct(){}
		
		static public function instance(){
			if(!isset(self::$instance))
			{
				self::$instance = new self();
			}
			return self::$instance;
		}
		
		protected function get($key){
			if(isset($this->values[$key])){
				return $this->values[$key];
			}
			return null;
		}
		protected function set($key, $value){
			$this->values[$key] = $value;
		}
		
		static function set_request(Request $request){
			self::$instance->set('request', $request);
		}
		
		static function get_request(){
			return self::$instance->get('request');
		}
	}
Registration form for session requests:


class SessionRegistry extends Registry{
		private $instance;
		
		 private function __construct(){
			session_start();
		 }
		 
		static function instance(){
			if(!isset(self::$instance)){
				self::$instance = new self();
			}
			return self::$instance;
		}
		
		protected function get($key){
			if(isset($_SESSION[__CLASS__][$key])){
				return $_SESSION[__CLASS__][$key]
			}
			return null;
		}
		
		protected function set($key, $value){
			$_SESSION[__CLASS__][$key] = $value;
		}
		
		public function set_complex(Complex $complex){
			self::$instance->set('complex', $complex);
		}
		
		public function get_complex(){
			return self::$instance->get('complex');
		}
	}
Supports application-level registries:


class ApplicationRegistry extends Registry{
		
		private $dir = "data";
		private static $instance;
		private $values = array();
		private $mtimes  = array();
		
		private function __construct(){}
		
		static function instance(){
			if(!isset(self::instance)){
				self::$instance = new self();
			}
			return self::$instance;
		}
		
		protected function set($key, $value){
			$this->values[$key] = $value;
			$path = $this->dir . DIRECTORY_SEPARATOR . $key;
			file_put_contents($path, serialize($value));
			$this->mtimes[$key] = time();
		}
		
		protected function get($key){
			$path = $this->dir . DIRECTORY_SEPARATOR . $key;
			if(file_exists($path)){
				$mtime = filetime($path);
				if(!isset($this->mtimes[$key]))	{$this->mtimes[$key] = 0;}
				if($mtime > $this->mtimes[$key]){
					$data = file_get_contents($path);
					$this->mtimes[$key] = $mtime;
					return ($this->values[$key] = unserialize($data));
				}
				if(isset($this->values[$key])){
					return $this->values[$key];
				}
			}
			return null;
		}
		
		static function get_dsn(){
			return self::$instance->get('dsn');
		}
		
		static function set_dsn($dsn){
			self::$instance->set('dsn', $dsn);
		}
	}
The end.


The above has introduced the registry mode, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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