单例模式
<?php
class Uni {
static private $instance;
private $config;
private function __construct($config) {
$this->config = $config;
}
// 防止对象被克隆
private function __clone()
{
// TODO: Implement __clone() method.
}
static public function getInstance($config) {
if (!self::$instance instanceof self) {
self::$instance = new Uni($config);
}
return self::$instance;
}
public function getName() {
echo $this->config;
}
}
$instance1 = Uni::getInstance("Leon");
echo $instance1->getName() . "\n";
$instance2 = Uni::getInstance("Ada");
echo $instance1->getName();
Leon
Leon
可以看出,一个对象只能被实例化一次。
原型模式
<?php
Abstract class Prototype
{
abstract function __clone();
}
class Map extends Prototype
{
public $width;
public $height;
public $sea;
public function setAttribute(array $attributes)
{
foreach ($attributes as $key => $val) {
$this->$key = $val;
}
}
public function __clone()
{
$this->sea = clone $this->sea; //对象类型需要使用clone执行深拷贝
// TODO: Implement __clone() method.
}
}
class Sea
{
}
$mapPrototype = new Map();
$attributes = array('width'=>40,'height'=>60,'sea'=>(new Sea));
$mapPrototype->setAttribute($attributes);
$new_map = clone $mapPrototype;
var_dump($mapPrototype);
var_dump($new_map);
观察者模式
观察者模式(Observer),当一个对象的状态发生改变时,依赖他的对象会全部收到通知,并自动更新。
场景:一个事件发生后,要执行一连串更新操作.传统的编程方式,就是在事件的代码之后直接加入处理逻辑,当更新得逻辑增多之后,代码会变得难以维护.这种方式是耦合的,侵入式的,增加新的逻辑需要改变事件主题的代码
观察者模式实现了低耦合,非侵入式的通知与更新机制
<?php
abstract class EventGenerator {
private $ObServers = [];
public function add(Observer $observers) {
$this->ObServers[] = $observers;
}
public function notify() {
foreach ($this->ObServers as $obServer) {
$obServer->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 EventGenerator
{
/**
* 触发事件
*/
public function trigger()
{
//通知观察者
$this->notify();
}
}
//创建一个事件
$event = new Event();
//为事件增加旁观者
$event->add(new ObServer1());
$event->add(new ObServer2());
//执行事件 通知旁观者
$event->trigger();
工厂模式
<?php
interface mysql
{
public function connect();
}
class mysqli2 implements mysql
{
public function connect()
{
echo 'mysqli';
}
}
class pdo2 implements mysql
{
public function connect()
{
echo 'pdo';
}
}
class mysqlFactory
{
static public function factory($class_name)
{
return new $class_name();
}
}
$obj = mysqlFactory::factory('pdo2');
$obj->connect();
策略模式
<?php
/**
* 定义接口
*/
interface car
{
public function run();
}
/**
* 接口算法实现
* @return [type] [description]
*/
class bmwCar implements car
{
public function run()
{
echo "宝马汽车在路上奔驰\n";
}
}
/**
* 接口算法实现
* @return [type] [description]
*/
class audiCar implements car
{
public function run()
{
echo "奥迪汽车在路上奔驰\n";
}
}
/**
* 使用不同算法的类
* @param integer $speed [description]
*/
class chooseCar
{
public $speed;
function __construct($speed = 60)
{
$this->speed = $speed;
}
function start($brand)
{
$car = null;
switch ($brand) {
case "bmw":
$car = new bmwCar();
break;
case "audi":
$car = new audiCar();
break;
default:
$car = new bmwCar();
}
$car->run();
echo "时速为{$this->speed}km/h";
}
}
$car = new chooseCar(180);
$car->start("audi");
$car->start("bmw");
注册模式
注册器模式就是将对象注册到全局树上,那么他就可以被任意地方访问。
<?php
class Register
{
static protected $objects;//全局树 array
//设置
static function set($alias, $object)
{
self::$objects[$alias] = $object;
}
//获得
static function get($alias)
{
return self::$objects[$alias];
}
//注销
static function _unset($alias)
{
unset(self::$objects[$alias]);
}
}
Register::set('conf', array('dbhost' => '127.0.0.1'));
var_dump(Register::get('conf'));
Register::set('plane', array('shenzhen' => '12:00', 'beijing' => '15:00'));
var_dump(Register::get('plane'));
适配器模式
<?php
//目标角色
interface Target
{
public function simpleMethod1();
public function simpleMethod2();
}
//源角色
class Adaptee
{
public function simpleMethod1()
{
echo 'Adapter simpleMethod1' . "\n";
}
}
//类适配器角色
class Adapter implements Target
{
private $adaptee;
function __construct(Adaptee $adaptee)
{
$this->adaptee = $adaptee;
}
//委派调用Adaptee的sampleMethod1方法
public function simpleMethod1()
{
echo $this->adaptee->simpleMethod1();
}
public function simpleMethod2()
{
echo 'Adapter simpleMethod2' . "\n";
}
}
//客户端
class Client
{
public static function main()
{
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
$adapter->simpleMethod1();
$adapter->simpleMethod2();
}
}
Client::main();
装饰器模式
<?php
/**
* 输出一个字符串
* 装饰器动态添加功能
* Class EchoText
*/
class EchoText
{
protected $decorator = [];
public function Index()
{
//调用装饰器前置操作
$this->beforeEcho();
echo "你好,我是装饰器。";
//调用装饰器后置操作
$this->afterEcho();
}
//增加装饰器
public function addDecorator(Decorator $decorator)
{
$this->decorator[] = $decorator;
}
//执行装饰器前置操作 先进先出原则
protected function beforeEcho()
{
foreach ($this->decorator as $decorator)
$decorator->before();
}
//执行装饰器后置操作 先进后出原则
protected function afterEcho()
{
$tmp = array_reverse($this->decorator);
foreach ($tmp as $decorator)
$decorator->after();
}
}
/**
* 装饰器接口
* Class Decorator
*/
interface Decorator
{
public function before();
public function after();
}
/**
* 颜色装饰器实现
* Class ColorDecorator
*/
class ColorDecorator implements Decorator
{
protected $color;
public function __construct($color)
{
$this->color = $color;
}
public function before()
{
echo "<dis style='color: {$this->color}'>";
}
public function after()
{
echo "</div>";
}
}
/**
* 字体大小装饰器实现
* Class SizeDecorator
*/
class SizeDecorator implements Decorator
{
protected $size;
public function __construct($size)
{
$this->size = $size;
}
public function before()
{
echo "<dis style='font-size: {$this->size}px'>";
}
public function after()
{
echo "</div>";
}
}
//实例化输出类
$echo = new EchoText();
//增加装饰器
$echo->addDecorator(new ColorDecorator('red'));
//增加装饰器
$echo->addDecorator(new SizeDecorator('22'));
//输出
$echo->Index();