Strategy PHP design mode strategy policy mode

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.