亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php設(shè)計(jì)模式 Strategy(策略模式)

 更新時(shí)間:2011年06月26日 10:52:54   投稿:mdxy-dxy  
定義一系列算法,把它們一個(gè)個(gè)封裝起來,并且使它們可相互替換,使用得算法的變化可獨(dú)立于使用它的客戶

抽象策略(Strategy)角色:定義所有支持的算法的公共接口。通常是以一個(gè)接口或抽象來實(shí)現(xiàn)。Context使用這個(gè)接口來調(diào)用其ConcreteStrategy定義的算法。

具體策略(ConcreteStrategy)角色:以Strategy接口實(shí)現(xiàn)某具體算法。

環(huán)境(Context)角色:持有一個(gè)Strategy類的引用,用一個(gè)ConcreteStrategy對(duì)象來配置

核心代碼

<?php
interface Strategy { // 抽象策略角色,以接口實(shí)現(xiàn)
  public function algorithmInterface(); // 算法接口
}

class ConcreteStrategyA implements Strategy { // 具體策略角色A 
  public function algorithmInterface() {}
}

class ConcreteStrategyB implements Strategy { // 具體策略角色B 
  public function algorithmInterface() {}
}

class ConcreteStrategyC implements Strategy { // 具體策略角色C
  public function algorithmInterface() {}
}

class Context { // 環(huán)境角色
  private $_strategy;
  public function __construct(Strategy $strategy) {
    $this->_strategy = $strategy;
  } 
  public function contextInterface() {
    $this->_strategy->algorithmInterface();
  }
}

// client
$strategyA = new ConcreteStrategyA();
$context = new Context($strategyA);
$context->contextInterface();

$strategyB = new ConcreteStrategyB();
$context = new Context($strategyB);
$context->contextInterface();

$strategyC = new ConcreteStrategyC();
$context = new Context($strategyC);
$context->contextInterface();

其他代碼

<?php 
/** 
* 策略模式(Strategy.php) 
* 
* 定義一系列算法,把它們一個(gè)個(gè)封裝起來,并且使它們可相互替換,使用得算法的變化可獨(dú)立于使用它的客戶 
* 
*/ 

// ---以下是一系列算法的封閉---- 
interface CacheTable 
{ 
public function get($key); 
public function set($key,$value); 
public function del($key); 
} 

// 不使用緩存 
class NoCache implements CacheTable 
{ 
public function __construct(){ 
echo "Use NoCache<br/>"; 
} 

public function get($key) 
{ 
return false; 
} 

public function set($key,$value) 
{ 
return true; 
} 

public function del($key) 
{ 
return false; 
} 
} 

// 文件緩存 
class FileCache implements CacheTable 
{ 
public function __construct() 
{ 
echo "Use FileCache<br/>"; 
// 文件緩存構(gòu)造函數(shù) 
} 

public function get($key) 
{ 
// 文件緩存的get方法實(shí)現(xiàn) 
} 

public function set($key,$value) 
{ 
// 文件緩存的set方法實(shí)現(xiàn) 
} 

public function del($key) 
{ 
// 文件緩存的del方法實(shí)現(xiàn) 
} 
} 

// TTServer 
class TTCache implements CacheTable 
{ 
public function __construct() 
{ 
echo "Use TTCache<br/>"; 
// TTServer緩存構(gòu)造函數(shù) 
} 

public function get($key) 
{ 
// TTServer緩存的get方法實(shí)現(xiàn) 
} 

public function set($key,$value) 
{ 
// TTServer緩存的set方法實(shí)現(xiàn) 
} 

public function del($key) 
{ 
// TTServer緩存的del方法實(shí)現(xiàn) 
} 
} 

// -- 以下是使用不用緩存的策略 ------ 
class Model 
{ 
private $_cache; 
public function __construct() 
{ 
$this->_cache = new NoCache(); 
} 

public function setCache($cache) 
{ 
$this->_cache = $cache; 
} 
} 

class UserModel extends Model 
{ 
} 

class PorductModel extends Model 
{ 
public function __construct() 
{ 
$this->_cache = new TTCache(); 
} 
} 

// -- 實(shí)例一下 --- 
$mdlUser = new UserModel(); 
$mdlProduct = new PorductModel(); 
$mdlProduct->setCache(new FileCache()); // 改變緩存策略 
?>

 具體的大家可以多關(guān)注一下腳本之家以前發(fā)布的文章

相關(guān)文章

  • 深入for,while,foreach遍歷時(shí)間比較的詳解

    深入for,while,foreach遍歷時(shí)間比較的詳解

    本篇文章是對(duì)for,while,foreach遍歷時(shí)間比較進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 解析php中array_merge與array+array的區(qū)別

    解析php中array_merge與array+array的區(qū)別

    本篇文章是對(duì)php中array_merge與array+array的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 調(diào)整優(yōu)化您的LAMP應(yīng)用程序的5種簡單方法

    調(diào)整優(yōu)化您的LAMP應(yīng)用程序的5種簡單方法

    Wikipedia、Facebook 和 Yahoo! 等主要 web 屬性使用 LAMP 架構(gòu)來為每天數(shù)百萬的請(qǐng)求提供服務(wù),而 Wordpress、Joomla、Drupal 和 SugarCRM 等 web 應(yīng)用程序軟件使用其架構(gòu)來讓組織輕松部署基于 web 的應(yīng)用程序。
    2011-06-06
  • PHP應(yīng)用代碼復(fù)雜度檢測使用方法

    PHP應(yīng)用代碼復(fù)雜度檢測使用方法

    這篇文章主要為大家介紹了PHP應(yīng)用代碼復(fù)雜度檢測使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 一道關(guān)于php變量引用的面試題

    一道關(guān)于php變量引用的面試題

    當(dāng)一個(gè)變量等于另一個(gè)變量的引用的時(shí)候,這時(shí)任何一方改變了其值,另一方看到的這個(gè)值也會(huì)變化的。前加本次就表現(xiàn)出來,而后加下一次才會(huì)表現(xiàn)出來。
    2010-08-08
  • 最新評(píng)論