程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php設計模式之委托模式,php設計模式委托

php設計模式之委托模式,php設計模式委托

編輯:關於PHP編程

php設計模式之委托模式,php設計模式委托


委托模式是軟件設計模式中的一項基本技巧。在委托模式中,有兩個對象參與處理同一個請求,接受請求的對象將請求委托給另一個對象來處理。委托模式是一項基本技巧,許多其他的模式,如狀態模式、策略模式、訪問者模式本質上是在更特殊的場合采用了委托模式。
動態委托的介紹:動態委托概念來自於Jakarta 字節碼工程庫 (Byte-Code Engineering Library, BCEL)。它能夠分析存在的類,並且對於接口,抽象類,甚至運行時的具體類來說,它能夠生成以字節編碼委托類。
被委托的接口/類應該滿足如下條件:動態委托最多只能委托一個類,但是能夠代理多個接口。這個限制來自於Java的單繼承模式。一個Java類最多只有一個父類。既然生成的委托類把被委托類作為它的父類,那麼指定多個被委托類是不合理的。如果沒有指定被委托類,那麼缺省的父類就是Object。
下面是PHP 反射機制實現動態代理的代碼:

<?php
class Fruit

{

  function callFruit()

  {
    print "Generate an Apple";
  }

}

class FruitDelegator
{
 private $targets;
  function __construct()
  {
    $this->target[] = new Fruit();
  }
  
  function __call($name, $args)

  {
    foreach ($this->target as $obj)
    {
      $r = new ReflectionClass($obj);
      if ($method = $r->getMethod($name))

      {

        if ($method->isPublic() && !$method->isAbstract())
        {

          return $method->invoke($obj, $args);
        }

      }

    }
  }

}

$obj = new FruitDelegator();

$obj->callFruit();

// 運行結果

// Generate an Apple
?>

可見,通過代理類FruitDelegator來代替Fruit類來實現他的方法。
同樣的,如下的代碼也是能夠運行的:

<?php
class Color

{
  function callColor()
  {
    print "Generate Red";
  }
}
 

class ColorDelegator

{

  private $targets;

   

  function addObject($obj)

  {

    $this->target[] = $obj;

  }

   

  function __call($name, $args)

  {

    foreach ($this->target as $obj)

    {

      $r = new ReflectionClass($obj);

      if ($method = $r->getMethod($name))

      {

        if ($method->isPublic() && !$method->isAbstract())

        {
          return $method->invoke($obj, $args);
        }
      }
    }
  }
}

$obj = new ColorDelegator();
$obj->addObject(new Color());
$obj->callColor();

?>


設計了一個cd類,類中有mp3播放模式,和mp4播放模式
改進前,使用cd類的播放模式,需要在實例化的類中去判斷選擇什麼方式的播放模式
改進後,播放模式當做一個參數傳入playList函數中,就自動能找到對應需要播放的方法。

一、未改進前

<?php 
//使用委托模式之前,調用cd類,選擇cd播放模式是復雜的選擇過程 
class cd { 
 protected $cdInfo = array(); 
  
 public function addSong($song) { 
  $this->cdInfo[$song] = $song; 
 } 
  
 public function playMp3($song) { 
  return $this->cdInfo[$song] . '.mp3'; 
 } 
  
 public function playMp4($song) { 
  return $this->cdInfo[$song] . '.mp4'; 
 } 
} 
$oldCd = new cd; 
$oldCd->addSong("1"); 
$oldCd->addSong("2"); 
$oldCd->addSong("3"); 
$type = 'mp3'; 
if ($type == 'mp3') { 
 $oldCd->playMp3(); 
} else { 
 $oldCd->playMp4(); 
}

二、通過委托模式,改進後的cd類

<?php
 
namespace Tools;
 
/*
委托模式
去除核心對象中的判決和復雜功能性
*/
 
//委托接口
interface Delegate{
 public function playList($list,$song);
}
 
//mp3處理類
class mp3 implements Delegate{
 public function playList($list,$song){
  return $list[$song].'.mp3';
 }
}
 
//mp4處理類
class mp4 implements Delegate{
 public function playList($list, $song)
 {
  return $list[$song].'.mp4';
 }
}
 
class cdDelegate{
 protected $cdInfo = array();
 
 public function addSong($song){
  $this->cdInfo[$song] = $song;
 }
 
 public function play($type,$song){
  $name = '\Tools\\'.$type;
  $obj = new $name;
  return $obj->playList($this->cdInfo,$song);
 }
}
 
$newCd = new cdDelegate();
$newCd->addSong("1");
$newCd->addSong("2");
$newCd->addSong("3");
echo $newCd->play('mp3','1');//只要傳遞參數就能知道需要選擇何種播放模式

再為大家分享一個實例:

<?php
/**
 * 委托模式 示例
 *
 * @create_date: 2010-01-04
 */
class PlayList
{
 var $_songs = array();
 var $_object = null;
 
 function PlayList($type)
 {
  $object = $type."PlayListDelegation";
  $this->_object = new $object();
 }
 
 function addSong($location,$title)
 {
  $this->_songs[] = array("location"=>$location,"title"=>$title);
 }
 
 function getPlayList()
 {
  return $this->_object->getPlayList($this->_songs);
 }
}
 
class mp3PlayListDelegation
{
 function getPlayList($songs)
 {
  $aResult = array();
  foreach($songs as $key=>$item)
  {
   $path = pathinfo($item['location']);
   if(strtolower($item['extension']) == "mp3")
   {
    $aResult[] = $item;
   }
  }
  return $aResult;
 }
}
 
class rmvbPlayListDelegation
{
 function getPlayList($songs)
 {
  $aResult = array();
  foreach($songs as $key=>$item)
  {
   $path = pathinfo($item['location']);
   if(strtolower($item['extension']) == "rmvb")
   {
    $aResult[] = $item;
   }
  }
  return $aResult;
 }
}
 
$oMP3PlayList = new PlayList("mp3");
$oMP3PlayList->getPlayList();
$oRMVBPlayList = new PlayList("rmvb");
$oRMVBPlayList->getPlayList();
?>

以上就是本文的全部內容,希望對大家的學習有所幫助。

您可能感興趣的文章:

  • php設計模式 Delegation(委托模式)
  • PHP設計模式之裝飾者模式
  • php中使用接口實現工廠設計模式的代碼
  • PHP最常用的2種設計模式工廠模式和單例模式介紹
  • php設計模式小結
  • php設計模式之單例模式使用示例
  • php設計模式之命令模式使用示例
  • PHP設計模式之觀察者模式(Observer)詳細介紹和代碼實例
  • php設計模式之簡單工廠模式詳解
  • PHP常用設計模式之委托設計模式

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved