Copy CodeThe code is as follows:
/**
* Strategy Mode (strategy.php)
*
* Define a series of algorithms, encapsulate them one by one, and make them interchangeable, and the algorithm used can be changed independently of the customers who use it
*
*/
---The following are closed----for 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
";
}
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
";
File Cache Constructors
}
Public function Get ($key)
{
Get method implementation of file cache
}
Public function set ($key, $value)
{
Implementation of Set method for file cache
}
Public Function del ($key)
{
The Del method implementation of the file cache
}
}
Ttserver
Class Ttcache implements Cachetable
{
Public Function __construct ()
{
echo "Use Ttcache
";
Ttserver Cache Constructors
}
Public function Get ($key)
{
Ttserver Cache's Get method implementation
}
Public function set ($key, $value)
{
Implementation of set method for Ttserver cache
}
Public Function del ($key)
{
Ttserver Cache's Del method implementation
}
}
--The following policies are used 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 ();
}
}
--Example---
$mdlUser = new Usermodel ();
$mdlProduct = new Porductmodel ();
$mdlProduct->setcache (New Filecache ()); Changing the cache policy
?>
The above describes the strategy PHP design mode strategy strategy mode, including the strategy aspect of the content, I hope that the PHP tutorial interested in friends to help.