Copy codeThe Code is as follows: <? Php
/**
* Strategy Mode (Strategy. php)
*
* Define a series of algorithms, encapsulate them one by one, and make them replace each other. Changes in the used algorithms can be independent of customers who use them.
*
*/
// --- The following is the closure of a series of algorithms ----
Interface CacheTable
{
Public function get ($ key );
Public function set ($ key, $ value );
Public function del ($ key );
}
// Do not use Cache
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;
}
}
// File Cache
Class FileCache implements CacheTable
{
Public function _ construct ()
{
Echo "Use FileCache <br/> ";
// File Cache Constructor
}
Public function get ($ key)
{
// Get Method for File Cache
}
Public function set ($ key, $ value)
{
// Set method implementation of File Cache
}
Public function del ($ key)
{
// Implement the del Method for File Cache
}
}
// TTServer
Class TTCache implements CacheTable
{
Public function _ construct ()
{
Echo "Use TTCache <br/> ";
// TTServer cache Constructor
}
Public function get ($ key)
{
// Get method implementation of TTServer Cache
}
Public function set ($ key, $ value)
{
// Set method implementation of TTServer Cache
}
Public function del ($ key)
{
// Del Implementation of TTServer Cache
}
}
// -- The following is a policy without caching ------
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 ();
}
}
// -- Instance ---
$ MdlUser = new UserModel ();
$ MdlProduct = new PorductModel ();
$ MdlProduct-> setCache (new FileCache (); // modify the Cache Policy
?>