Php design pattern Strategy (policy pattern)

Source: Internet
Author: User
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. Copy codeThe code is as follows:
/**
* 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
";
}

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 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
";
// 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
?>

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.