标签:method mys 状态 ret mysqli gis 交通 策略模式 roo
这些设计模式在常见的php框架:ThinkPHP, Yii2等都有用到.
/*
* 单例模式
* 确保此类只有一个实例并可以自行实例化
* 用在: 数据库操作, 日志对象 等.
*/
class single{
public $db = NULL;
static $instance = null;
public function __construct() {
$this->db = new mysqli(‘127.0.0.1‘, ‘root‘, ‘root‘, ‘test‘);
if($this->db->connect_error){
die(‘connect error:‘ . $this->db->connect_error);
}
echo "mysql connect successful\n";
}
static public function getInstance(){
if(!(self::$instance instanceof self)){
self::$instance = new self();
echo "aaa\n";
}
echo "bbb\n";
return self::$instance;
}
public function __destruct() {
$this->db->close();
}
}
single::getInstance();
single::getInstance();
//输出:
/*
mysql connect successful
aaa
bbb
bbb
*/
/**
* 工厂模式
* 用工厂方法代替new操作的一种模式
*/
interface people{
public function say();
}
class man implements people{
public function say() {
echo "i am man \n";
}
}
class woman implements people{
public function say() {
echo "i am woman \n";
}
}
class factory{
public function createMan(){
return new man();
}
public function createWoman(){
return new woman();
}
}
$f = new factory();
$f->createMan()->say();
$f->createWoman()->say();
//输出
/*
i am man
i am woman
*/
/**
* 策略模式 [使用不同的方式达到同一个目的]
* 场景:乘坐不同交通工具去公司上班
*/
abstract class tools{
abstract function working();
}
class bicyle extends tools{
public function working() {
echo __METHOD__ . "\n";
}
}
class car extends tools{
public function working() {
echo __METHOD__ . "\n";
}
}
class transport{
public function running($obj){
$obj->working();
}
/**
* 使用魔术方法也可以达到上述效果
* @param type $name
* @param type $arguments
*/
public function __call($name, $arguments) {
$arguments[0]->$name();
}
}
$t = new transport();
$t->running(new car());
$t->running(new bicyle());
$t->working(new car());//使用魔术方法也可以达到上述效果
//输出
/*
car::working
bicyle::working
car::working
*/
/*
* 观察者模式
* 当一个对象状态发生变化时,依赖它的对象全部收到触发操作
* 发生场景:yii2的behavior,event
*/
interface event{
function run();
}
class e1 implements event{
public function run() {
echo __METHOD__ . "\n";
}
}
class e2 implements event{
public function run() {
echo __METHOD__ . "\n";
}
}
class observer{
static $obs;
function addObs($name, $obj){
self::$obs[$name] = $obj;
}
function trigger(){
foreach (self::$obs as $val){
$val->run();
}
}
}
$o = new observer();
$o->addObs(‘e1‘, new e1());
$o->addObs(‘e2‘, new e2());
$o->trigger();
//输出
/*
e1::run
e2::run
*/
/*
* 适配器模式
* 把不同的方法封装成统一的方法以供调用
* 用在 数据库 mysql, mysqli, pdo 等
*/
interface db{
public function connect();
public function query();
public function close();
}
class mysqlA implements db{
public $db;
public function connect(){
$this->db = mysqli_connect($host, $user, $password, $database, $port, $socket);
}
public function query(){
}
public function close(){
mysqli_close($this->db);
}
}
class mysqlB implements db{
public $db;
public function connect() {
$this->db = new PDO($dsn, $username, $password);
}
public function query(){
}
public function close(){
$this->db = null;
}
}
/*
* 注册模式
* 把已有对象放入一个数组里以供调用
*/
class register{
static $objects;
public static function set($name, $obj){
self::$objects[$name] = $obj;
}
public static function get($name){
return self::$objects[$name];
}
}
class log{};
register::set(‘log‘, new log());
$log = register::get(‘log‘);
标签:method mys 状态 ret mysqli gis 交通 策略模式 roo
原文地址:https://www.cnblogs.com/kaykay012/p/9599355.html