php 常用设计模式demo

<?php
//__get()
//__set()当对象中属性不存在时调用该魔术方法
//__call()当对象中方法不存在时
//__callStatic()静态方法
//__string()当对象不能转换成字符串时使用__string()
//__invoke()对象不能直接当成一个函数使用
//工厂模式,是有一个工厂创建类,通过工厂调用:: 工厂方法或者类生成对象,而不是在代码中直接new
//单例模式,一个类只有一个对象,例如要连接数据库使用一次就可以了,使某个类的对象仅允许创建一次
//注册模式,全局共享和交换对象

//单例设计模式
//设计模式讲究对象之间关系的抽象
//单例模式只有自己一个对象
//单例模式应用场景:
//1.应用程序与数据库交互
// 使用单例模式可以避免大量的new操作,每次new操作都会消耗内存资源和系统资源
//2.控制配置信息
//系统中需要有一个类来全局控制某些配置信息
//策略模式
//策略模式,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,
//实例:加入一个电商网站,针对男性女性用户要跳转到不同的电商类目,并且所有的广告位展示不同的广告
//可以实现Ioc,依赖导致,控制反转
/**
* 工厂模式例1
*/
interface person{
public function say();
}
class man implements person{
public function say(){
echo "我是男生<br>";
}
}
class woman implements person{
public function say(){
echo "我是女生<br>";
}
}
class simpleFactoty{
static function createman(){
return new man();
}
static function createwoman(){
return new woman();
}

}
$man = simpleFactoty::createman();
$man->say();
$woman = simpleFactoty::createwoman();
$woman->say();
/**
* 工厂模式例2
*/
interface transport{

public function go();

}

class bike implements transport{

public function go(){

echo "bike跑的慢<br>";

}

}

class car implements transport{

public function go(){

echo "car开的快<br>";

}

}

class bus implements transport{

public function go(){

echo "bus每一站都要停<br>";

}

}

class transFactory{

public static function factory($transport){

switch($transport){

case 'bike':

return new bike();

break;

case 'car':

return new car();

break;

case 'bus':

return new bus();

break;

}

}

}

$transport = transFactory::factory('car');
$transport->go();
/**
* 单例模式例1
*/
class Single {
private $name;//声明一个私有的实例变量
private function __construct(){//声明私有构造方法为了防止外部代码使用new来创建对象。
}
static public $instance;//声明一个静态变量(保存在类中唯一的一个实例)
static public function getinstance(){//声明一个getinstance()静态方法,用于检测是否有实例对象
if(!self::$instance)
self::$instance = new self();
return self::$instance;
}
public function setname($n){
$this->name = $n;
}
public function getname(){
return $this->name;
}
}
$oa = Single::getinstance();
$ob = Single::getinstance();
$oa->setname('hello world');
$ob->setname('good morning'.'<br>');
echo $oa->getname();//good morning
echo $ob->getname();//good morning

/**
* 单例模式例2
*/
class uni{
static private $instance;//创建静态私有的变量保存该类对象
private $config;//参数
private function __construct($config){
$this->config = $config;
echo "我被实例化了";
}
private function __clone(){

}
//提供一个访问这个实例的公共的静态方法(通常为getInstance方法),从而返回唯一实例的一个引用
static public function getInstance($config){//判断$instance是否是UNI的对象
if(!self::$instance instanceof self){//判断一个对象是否是某个类的实例
self::$instance = new self($config);
}
return self::$instance;
}
public function getName(){
echo $this->config;
}


}
$db1 = uni::getInstance(1);
$db1->getName();
echo "<br>";
$db2 = uni::getInstance(4);
$db2->getName();
echo "<br>";
/**
* 单例模式例3
*/

class pzhang{

static private $instance;

private $config;

private function __construct($config){

$this->config = $config;

echo "我被实例化了";

}

private function __clone(){

}

static public function getInstance($config){

if(!self::$instance instanceof self)

self::$instance = new self($config);

return self::$instance;

}

public function getname(){

echo $this->config;

}

}

$adb = pzhang::getInstance(1);

$adb->getname();
echo "<br>";
/**
* 策略模式
*/
interface baseAgent{//抽象策略角色
public function do_method();
}
class ConcreteStrategyA implements baseAgent{//具体策略角色
public function do_method(){
echo "do method a";
}
}
class ConcreteStrategyB implements baseAgent{//具体策略角色
public function do_method(){
echo "do method b";
}
}
class ConcreteStrategyC implements baseAgent{//具体策略角色
public function do_method(){
echo "do method b";
}
}
class Question{ // 环境角色
private $__strategy;

public function __construct(BaseAgent $baseAgent) {
$this->__strategy = $baseAgent;
}
public function handle_question() {
$this->__strategy->do_method();
}
}
$strategyA = new ConcreteStrategyA();
$question = new Question($strategyA);
$question->handle_question();//输出do method A

$strategyB = new ConcreteStrategyB();
$question = new Question($strategyB);
$question->handle_question();//输出do method B

$strategyC = new ConcreteStrategyC();
$question = new Question($strategyC);
$question->handle_question();//输出do method C
echo "<br>";
/**
* 策略模式
*/

interface pzhangt{//抽象策略角色

public function say();

}

class A implements pzhangt{//具体策略角色

public function say(){

echo "说see you";

}

}

class B implements pzhangt{

public function say(){

echo "说 good bay";

}

}

class questiont{

private $test;

public function __construct(Pzhangt $pzhangt){

$this->test = $pzhangt;

}

public function handle(){

$this->test->say();

}

}

$a = new A();

$res = new questiont($a);

$res->handle();

$b = new B();

$res = new questiont($b);

$res->handle();


?>
原文地址:https://www.cnblogs.com/isuansuan/p/9755509.html