//观察者模式
//适用场景:订阅者通知
//自己对观察者模式对理解:
需求:有事情变化你要通知我
实现:
1、要通知对人必须在我这里注册,否则我不知道要通知谁。
2、想要获取通知的人必须遵守我的规则,实现我指定的通知事件,不然我没办法统一通知,(比如高考查分数,只支持电话查询,你非要微信查我肯定通知不到你)
3、事件发生的时候我会逐一通知你们。
<?php
//定义一个事件产生接口
abstract class genEvent
{
private $ob_servers = [];
//增加观察者
public function addObs($ob_server)
{
$this->ob_servers[] = $ob_server;
}
//通知
public function notify()
{
if(!empty($this->ob_servers))
{
foreach($this->ob_servers as $ob_server)
{
$ob_server->update();
}
}
}
}
//定义观察者接口
interface obServer{
public function update($event_info = null);
}
class obServer1 implements obServer{
public function update($event_info = null){
echo "观察者1 收到执行通知\n";
}
}
class obServer2 implements obServer{
public function update($event_info = null){
echo "观察者2 收到执行通知\n";
}
}
class event extends genEvent{
//事件触发
public function trigger()
{
$this->notify();
}
}
//实现
$event = new event();
$event->addObs(new obServer1());
$event->addObs(new obServer2());
$event->trigger();
?>
//简单工厂模式
//适用场景:创建对象比较少,业务逻辑不太复杂
<?php
//产品
class bike
{
public function product($destination)
{
print_r($destination);
}
}
class car
{
public function product($destination)
{
print_r($destination);
}
}
//工厂
class simpleFactory
{
public function createBike($obj)
{
return new $obj();
}
}
$factory = new simpleFactory();
$bike = $factory->createBike("bike");
$bike->product("hello,bike");
$car = $factory->createBike("car");
$car->product("hello,car");
?>
适配模式
<?php
//定义抽象类
abstract class Toy
{
public abstract function openMouth();
public abstract function closeMouth();
}
//定义dog
class dog extends Toy
{
public function openMouth()
{
echo "Dog open Mouth\n";
}
public function closeMouth()
{
echo "Dog close Mouth\n";
}
}
//cat
class cat extends Toy
{
public function openMouth()
{
echo "Cat open Mouth\n";
}
public function closeMouth()
{
echo "Cat close Mouth\n";
}
}
//红枣狗可以自己判断开关
interface redTarget
{
public function doMouthOpen();
public function doMouthClose();
}
interface greenTarget
{
public function operateMouth($type=0);
}
//组成适配器
class redAdapter implements redTarget
{
private $adaptee;
//初始化对象
public function __construct(Toy $adaptee)
{
$this->adaptee = $adaptee;
}
//委派调用Adaptee的sampleMethod1方法
public function doMouthOpen()
{
$this->adaptee->openMouth();
}
public function doMouthClose()
{
$this->adaptee->closeMouth();
}
}
//组成绿色遥控
class greenAdapter implements greenTarget
{
private $adapter;
//初始化对象
public function __contruct(Toy $adapter)
{
$this->adapter = $adapter;
}
public function operateMouth($type = 0)
{
$type ? $this->adapter->openMouth() : $this->adapter->closeMouth();
}
}
//测试
class testDriver
{
public function run()
{
//实例化玩具狗
$dog = new dog();
$adapter_dog = new redAdapter($dog);
$adapter_dog->doMouthOpen();
$adapter_dog->doMouthClose();
}
}
$test = new testDriver();
$test->run();
?>
DB类适配模式
<?php
//db adapter
interface DBInterface{
//连接
public function connect($host,$user,$passwd,$dbname);
//查询
public function query($sql);
//关闭
public function close();
}
//mysql
class MySQL implements DBInterface{
protected $conn;
public function connect($host,$user,$passwd,$dbname)
{
$this->conn = mysql_connect($host,$user,$passwd);
mysql_select_db($dbname);
return $this->conn;
}
public function query($sql)
{
$res = mysql_query($sql,$this->conn);
return $res;
}
public function close()
{
mysql_close($this->conn);
}
}
//pdo
class newPDO implements DBInterface{
protected $conn;
public function connect($host,$user,$passwd,$dbname)
{
$this->conn = new Pdo("mysql:host=$host;dbname=$dbname",$user,$passwd);
}
public function query($sql)
{
return $this->conn->query($sql);
}
public function close()
{
unset($this->conn);
}
}
//测试
// $db = new MySQL();
// $rs = $db->connect('127.0.0.1','mysql','nopass','activity');
// $total = $db->query("select count(*) from activity_cron");
$db = new newPDO();
$rs = $db->connect('127.0.0.1','mysql','nopass.2','activity');
$total = $db->query("select count(*) from activity_cron");
?>
//策略模式
<?php
namespace model;
//定义接口
interface userStrategy{
//显示广告
public function showAd();
//显示分类
public function showCa();
}
//实现
class femaleStrategy implements userStrategy{
//show
public function showAd(){
echo '2019新款';
}
public function showCa(){
echo '服装';
}
}
class maleStrategy implements userStrategy{
//show
public function showAd(){
echo '宝马';
}
//显示目录
public function showCa(){
echo '汽车';
}
}
//业务场景
class show {
public $strategy;
//设置
public function setStrategy($userStrategy){
$this->setStrategy = $userStrategy;
}
public function index(){
$this->setStrategy->showAd();
$this->setStrategy->showCa();
}
}
$a = new show();
$sex = 'f';
if($sex == 'f')
{
$sf = new femaleStrategy();
$a->setStrategy($sf);
$a->index();
}
else
{
$sf = new maleStrategy();
$a->setStrategy($sf);
$a->index();
}
?>
//状态模式,积分等级判断
<?php
//积分等级判断
interface istate{
public function grade(State $param);
}
class golden implements istate{
public function grade(State $a)
{
if($a->score > 0 && $a->score < 1000)
{
echo "{$a->score}点积分,黄金会员";
}
else
{
//超出了等级
$a->setState(new platinum());
$a->grade();
}
}
}
class platinum implements istate{
public function grade(State $a)
{
if($a->score >= 1000 && $a->score < 2000)
{
echo "{$a->score}点积分,白金会员";
}
else
{
//超出了等级
$a->setState(new diamond());
$a->grade();
}
}
}
class diamond implements istate{
public function grade(State $a)
{
if($a->score >= 2000 )
{
echo "{$a->score}点积分,钻石会员";
}
}
}
class state
{
private $first;
public function __construct()
{
$this->first = new golden();
}
public function setState(istate $obj)
{
$this->first = $obj;
}
public function grade()
{
$this->first->grade($this);
}
}
$b = new state();
$b->score = 1000;
$b->grade();
?>
//装饰模式
<?php
class CD{
public $trackList;
public function __construct()
{
$this->trackList = [];
}
public function addTrack($track)
{
$this->trackList = $track;
}
public function getTrack()
{
$output = "";
foreach($this->trackList as $key=>$val)
{
$output .= ($key+1) . ":{$val}";
}
return $output;
}
}
//变化后的需求,需要把所有的输出采用大写
class cdToUpper {
private $_cd;
public function __construct(CD $CD)
{
$this->_cd = $CD;
}
public function makeCaps()
{
foreach($this->_cd->trackList as $key=>$val)
{
$this->_cd->trackList[$key] = strtoupper($val);
}
}
}
//实例化
$my_cd = new CD();
$trackList = [
'what is your name',
'hanmeimei',
'lilei',
];
//初始化
$my_cd->addTrack($trackList);
$cd_to_upper = new cdToUpper($my_cd);
$cd_to_upper->makeCaps();
$list = $my_cd->getTrack();
echo '<pre>';
print_r($list);exit;
?>