Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

11.30. Design pattern (设计模式)

常用设计模式包括

Singleton 单件模式
Abstract Factory 抽象工厂模式
Builder 生成器模式
Factory Method 工厂方法模式
Prototype 原型模式
Adapter 适配器模式
Bridge 桥接模式
Composite 组合模式
Decorator 装饰模式
Facade 外观模式
Flyweight 享元模式
Proxy 代理模式
Template Method模板方法
Command 命令模式
Interpreter 解释器模式
Mediator 中介者模式
Iterator 迭代器模式
Observer 观察者模式
Chain Of Responsibility 职责链模式
Memento 备忘录模式
State 状态模式
Strategy 策略模式
Visitor 访问者模式
	

11.30.1. Singleton 单件模式

		
<?php
class Cache {

	private $cache = array();
	public function __construct(){}
	public function set($key,$value){
		if(!empty($key)){
			$this->cache[$key] = $value;
		}
	}
	public function get($key){
		if(array_key_exists($key, $this->cache)){
			print($this->cache[$key]);
		}
	}

}
		
		
		
<?php
class Cache {

	private static $instance;
	private $cache = array();
	private function __construct(){}
	public static function getInstance() {
		if(empty( self::$instance )){
			self::$instance = new Cache();
		}
		return self::$instance;
	}
	public function set($key,$value){
		if(!empty($key)){
			$this->cache[$key] = $value;
		}
	}
	public function get($key){
		if(array_key_exists($key, $this->cache)){
			print($this->cache[$key]);
		}
	}

}

$db = Cache::getInstance();
$db->set('name','netkiller');
$db->get('name');
print("\r\n");

$db1 = Cache::getInstance();
$db1->get('name');
$db1->set('age','30');
print("\r\n");

$db2 = Cache::getInstance();
$db2->get('name');
$db2->get('age');
print("\r\n");

unset($db1);

$db->set('name','neo');
$db->get('age');
$db2->get('name');
print("\r\n");

print("---------------------------\r\n");
// private function __construct(){}
//$db3 = new Cache();
//$db3->set('name','netkiller');

//$db1 = new Cache()
//$db1->get('name');