- 单例模式
<?php
class mysingle
{
private static $myinstance =null;
private function __construct()
{
}
private function __clone()
{
}
public static function createobject()
{
if (self::$myinstance==null)
{
self::$myinstance = new self();
}
return self::$myinstance;
}
}
$thesingle = mysingle::createobject();
$anothersingle = mysingle::createobject();
echo ($thesingle===$anothersingle);
?>
输出结果:
1
说明两个对象完全一致
- 观察者模式
被观察者(dingdaguo)做出某种行为(drink_cola())时,触发监控者(coach)的做出一些反应(persuade)
<?php class dingdaguo { protected $observers = []; public function addobserver($myobserver) { $this->observers[] = $myobserver; } public function drink_cola() { foreach($this->observers as $observer) { $observer->persuade(); } } public function delobsrver($observer) { $index = array_search($observer,$this->observers,); $this->observers=array_slice($this->observers,0,$index); } } class coach { public function __construct($name) { $this->name = $name; } public function persuade() { echo $this->name."说:别喝可乐了,都是甜的东西".PHP_EOL; } } $cpc = new coach("陈培昌"); $cj = new coach("程劲"); $dy = new dingdaguo(); $dy->addobserver($cpc); $dy->addobserver($cj); $dy->drink_cola();
echo "删除其中一个对象".PHP_EOL;
$dy->delobsrver($cj);
$dy->drink_cola();
输出结果:
陈培昌说:别喝可乐了,都是甜的东西
程劲说:别喝可乐了,都是甜的东西
删除其中一个对象
陈培昌说:别喝可乐了,都是甜的东西
- 适配器
实现接口的过程中,所有类都按照一种方式实现接口
<?php interface martial_trainning { public function welcomeword(); public function braziljudo(); public function muaythai(); } class xxd { public function welcomeword() { echo "欢迎来到必图训练".PHP_EOL; } } class cj { public function braziljudo() { echo "我是小烤肠,来跟我一起学巴西柔术吧".PHP_EOL; } } class cpc implements martial_trainning { protected $myxxd = null; protected $mycj = null; public function __construct($xxd,$cj) { $this->myxxd = $xxd; $this->mycj = $cj; } public function welcomeword() { // TODO: Implement welcomeword() method. $this->myxxd->welcomeword(); } public function braziljudo() { // TODO: Implement braziljudo() method. $this->mycj->braziljudo(); } public function muaythai() { echo "我是小松鼠,跟我一起练习泰拳吧".PHP_EOL; } } $cj = new cj(); $xxd = new xxd(); $cpc = new cpc($xxd,$cj); $cpc->braziljudo(); $cpc->welcomeword(); $cpc->muaythai(); ?>
输出结果:
我是小烤肠,来跟我一起学巴西柔术吧
欢迎来到必图训练
我是小松鼠,跟我一起练习泰拳吧
- 策略模式
通过可以预测的参数传入,来决定调用哪个类的实现
<?php
interface martial_trainning
{
public function hoaring();
}
class xxd implements martial_trainning
{
public function hoaring()
{
echo "诶斯~诶斯~".PHP_EOL;
}
}
class cj implements martial_trainning
{
public function hoaring()
{
echo "诶~嘿嘿".PHP_EOL;
}
}
class cpc implements martial_trainning
{
public function hoaring()
{
echo "嗷儿喂~嗷儿喂~";
}
}
class mycoach
{
protected $myobject = null;
public function __construct($whichobject)
{
$this->myobject = $whichobject;
}
public function hoaring()
{
$this->myobject->hoaring();
}
}
$cpc = new cpc();
$mycoach = new mycoach($cpc);
$mycoach->hoaring();
echo PHP_EOL."===========换了一个类=============".PHP_EOL;
$cj = new cj();
$mycoach = new mycoach($cj);
$mycoach->hoaring();
?>
输出结果:
嗷儿喂~嗷儿喂~
===========换了一个类=============
诶~嘿嘿