1瞧掺、單例模式
一個(gè)類,只能允許有一個(gè)對象存在
<?php
class test{
protected function __construct(){
}
public static function getInstance(){
$_test = new test();
return $_test;
}
}
$test = test::getInstance();
var_dump($test);
?>
2凡傅、工廠模式
工廠模式辟狈,顧名思義,如同工廠一樣夏跷,你把原材料放入工廠中哼转,出來的是成品,而你并不需要知道工廠里做了什么槽华,工廠模式主要用于解耦壹蔓。
把對象的創(chuàng)建和使用的過程分開,比如: ClassA 調(diào)用 ClassB猫态,那么 ClassA 只調(diào)用ClassB 的方法庶溶,至于實(shí)例化 ClassB 則在工廠內(nèi)實(shí)現(xiàn)。這樣既減少了代碼的重復(fù)使用懂鸵,也方便對 ClassB 的后期維護(hù)。如果 ClassB 實(shí)例化過程很復(fù)雜行疏,使用簡單工廠模式就會發(fā)現(xiàn)外部無需關(guān)注復(fù)雜的實(shí)例化匆光,只管調(diào)用 ClassB 的方法即可,減少錯(cuò)誤
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();
3酿联、注冊模式
注冊模式终息,解決全局共享和交換對象夺巩。已經(jīng)創(chuàng)建好的對象,掛在到某個(gè)全局可以使用的數(shù)組上周崭,在需要使用的時(shí)候柳譬,直接從該數(shù)組上獲取即可。將對象注冊到全局的樹上续镇。任何地方直接去訪問美澳。
<?php
class Register
{
protected static $objects;
function set($alias,$object)//將對象注冊到全局的樹上
{
self::$objects[$alias]=$object;//將對象放到樹上
}
static function get($name){
return self::$objects[$name];//獲取某個(gè)注冊到樹上的對象
}
function _unset($alias)
{
unset(self::$objects[$alias]);//移除某個(gè)注冊到樹上的對象。
}
}
\Auto\Register::set('single',$single);
$single = \Auto\Register::get('single');
var_dump($single);
4摸航、適配器模式
將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另外一個(gè)接口制跟。
//目標(biāo)角色
interface Aims
{
public function newMethod1();
public function newMethod2();
}
//需要被適配的類(Adaptee)
Class Man
{
public function oldMethod1()
{
echo 'man';
}
public function oldMethod2()
{
echo '男人';
}
}
//需要被適配的類(Adaptee)
Class Woman
{
public function oldMethod1()
{
echo 'woman';
}
public function oldMethod2()
{
echo '女人';
}
}
//適配器,
Class Adapters implements Aims
{
private $adaptee;
public function __construct($adaptee)
{
$this->adaptee = $adaptee;
}
public function newMethod1()
{
//以少量的代碼對被適配者作出適配
echo 'sex :';
$this->adaptee->oldMethod1();
}
public function newMethod2()
{
echo 'sex name :';
$this->adaptee->oldMethod2();
}
}
$adapter1 = new Adapters(new Man);
$adapter1->newMethod1();
$adapter2 = new Adapters(new Woman);
$adapter2->newMethod2();
5酱虎、策略模式
這是一個(gè)男人和女人的問題雨膨,將一組特定的行為和算法封裝成類,以適應(yīng)某些特定的上下文環(huán)境读串。
UserStrategy.php
<?php
/*
* 聲明策略文件的接口聊记,約定策略包含的行為。
*/
interface UserStrategy
{
function showAd();
function showCategory();
}
FemaleUser.php
<?php
class FemaleUser implements UserStrategy
{
function showAd(){
echo "2016冬季女裝";
}
function showCategory(){
echo "女裝";
}
}
MaleUser.php
<?php
class MaleUser implements UserStrategy
{
function showAd(){
echo "IPhone6s";
}
function showCategory(){
echo "電子產(chǎn)品";
}
}
Page.php//執(zhí)行文件
<?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();
6恢暖、原型模式
不常用排监,大的對象類才使用,表現(xiàn)在clone
7胀茵、觀察者模式
從面向過程的角度來看社露,首先是觀察者向主題注冊,注冊完之后琼娘,主題再通知觀察者做出相應(yīng)的操作峭弟,整個(gè)事情就完了
/**
* 事件產(chǎn)生類
* Class EventGenerator
*/
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
*/
interface ObServer
{
public function update($event_info = null);
}
/**
* 觀察者1
*/
class ObServer1 implements ObServer
{
public function update($event_info = null)
{
echo "觀察者1 收到執(zhí)行通知 執(zhí)行完畢!\n";
}
}
/**
* 觀察者1
*/
class ObServer2 implements ObServer
{
public function update($event_info = null)
{
echo "觀察者2 收到執(zhí)行通知 執(zhí)行完畢脱拼!\n";
}
}
/**
* 事件
* Class Event
*/
class Event extends EventGenerator
{
/**
* 觸發(fā)事件
*/
public function trigger()
{
//通知觀察者
$this->notify();
}
}
//創(chuàng)建一個(gè)事件
$event = new Event();
//為事件增加旁觀者
$event->add(new ObServer1());
$event->add(new ObServer2());
//執(zhí)行事件 通知旁觀者
$event->trigger();