单例模式
单例模式解决的是如何在整个项目中创建唯一对象实例的问题。
- $_instance必须声明为静态的私有变量
- 构造函数和析构函数必须声明为私有,防止外部程序new 类从而失去单例模式的意义
- getInstance()方法必须设置为公有的,必须调用此方法 以返回实例的一个引用
- ::操作符只能访问静态变量和静态函数
- new对象都会消耗内存
- 使用场景:最常用的地方是数据库连接。
- 使用单例模式生成一个对象后, 该对象可以被其它众多对象所使用。
- 私有的__clone()方法防止克隆对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Singleton { private static $uniqueInstance = null;
protected function __construct() { }
final private function __clone() { }
public static function getInstance() { if (self::$uniqueInstance === null) { self::$uniqueInstance = new self; }
return self::$uniqueInstance; } }
|
工厂模式
工厂模式解决的是如何不通过new建立实例对象的方法
工厂方法或者类生成对象,而不是在代码中直接new。
使用工厂模式,可以避免当改变某个类的名字或者方法之后,在调用这个类的所有的代码中都修改它的名字或者参数。
1 2 3 4 5 6 7 8 9 10 11 12
| <?php class Factory{
static function createDatabase(){ $test = new Test1(); return $test; } }
|
适配器模式
将各种截然不同的函数接口封装成统一的API。
PHP中的数据库操作有MySQL,MySQLi,PDO三种,可以用适配器模式统一成一致,使不同的数据库操作,统一成一样的API。类似的场景还有cache适配器,可以将memcache,redis,file,apc等不同的缓存函数,统一成一致。
首先定义一个接口(有几个方法,以及相应的参数)。然后,有几种不同的情况,就写几个类实现该接口。将完成相似功能的函数,统一成一致的方法。
策略模式
策略模式,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境。
eg:假如有一个电商网站系统,针对男性女性用户要各自跳转到不同的商品类目,并且所有的广告位展示不同的广告。在传统的代码中,都是在系统中加入各种if else的判断,硬编码的方式。如果有一天增加了一种用户,就需要改写代码。使用策略模式,如果新增加一种用户类型,只需要增加一种策略就可以。其他所有的地方只需要使用不同的策略就可以。
首先声明策略的接口文件,约定了策略的包含的行为。然后,定义各个具体的策略实现类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| UserStrategy.php <?php
interface UserStrategy { function showAd(); function showCategory(); }
FemaleUser.php <?php require_once 'Loader.php'; class FemaleUser implements UserStrategy { function showAd(){ echo "2016冬季女装"; } function showCategory(){ echo "女装"; } }
MaleUser.php <?php require_once 'Loader.php'; class MaleUser implements UserStrategy { function showAd(){ echo "IPhone6s"; } function showCategory(){ echo "电子产品"; } }
Page.php <?php require_once 'Loader.php'; class Page { protected $strategy; function index(){ echo "AD"; $this->strategy->showAd(); echo "<br>"; echo "Category"; $this->strategy->showCategory(); echo "<br>"; } function setStrategy(UserStrategy $strategy){ $this->strategy=$strategy; } }
$page = new Page(); if(isset($_GET['male'])){ $strategy = new MaleUser(); }else { $strategy = new FemaleUser(); } $page->setStrategy($strategy); $page->index();
|
注册模式
注册模式,解决全局共享和交换对象。已经创建好的对象,挂在到某个全局可以使用的数组上,在需要使用的时候,直接从该数组上获取即可。将对象注册到全局的树上。任何地方直接去访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Register { protected static $objects; function set($alias,$object)//将对象注册到全局的树上 { self::$objects[$alias]=$object;//将对象放到树上 } static function get($name){ return self::$objects[$name];//获取某个注册到树上的对象 } function _unset($alias) { unset(self::$objects[$alias]);//移除某个注册到树上的对象。 } }
|
观察者模式
观察者模式(Observer),当一个对象状态发生变化时,依赖它的对象全部会收到通知,并自动更新。
场景:一个事件发生后,要执行一连串更新操作。传统的编程方式,就是在事件的代码之后直接加入处理的逻辑。当更新的逻辑增多之后,代码会变得难以维护。这种方式是耦合的,侵入式的,增加新的逻辑需要修改事件的主体代码。
观察者模式实现了低耦合,非侵入式的通知与更新机制。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
abstract class EventGenerator { private $ObServers = [];
public function add(ObServer $ObServer) { $this->ObServers[] = $ObServer; }
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();
|
原型模式
原型模式是先创建好一个原型对象,然后通过clone原型对象来创建新的对象。适用于大对象的创建,因为创建一个大对象需要很大的开销,如果每次new就会消耗很大,原型模式仅需内存拷贝即可。