PHP design mode 2 policy mode

Source: Internet
Author: User

<?PHP/** ****************************************************** * policy mode: Policy mode for a set of algorithms, each algorithm is encapsulated in a separate class with a common interface, * So that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client. * The strategy model separates behavior from the environment. The Environment class is responsible for maintaining and querying behavior classes, and various algorithms are available in specific policy classes. * Because the algorithm and the environment independent, the algorithm increases or decreases, the modification will not affect the environment and the client. * ********************************************************* *//** Data Source Save Method interface defines four basic ways of data source operation (increase, delete, change, check)*/InterfaceData_source { Public functionAdd ();  Public functionDelete ();  Public functionedit ();  Public functionfind ();}/** * Database Save method Specific class * A form of a specific data source interface, which is to save the data to the database*/classMysql_datasourceImplementsData_source { Public functionAdd () {Echo' MySQL Data add '; }     Public functionDelete () {Echo' MySQL data removal '; }     Public functionedit () {Echo' MySQL data modification '; }     Public functionfind () {Echo' MySQL data query '; }}/** * Oracle Save method Specific class * Another form of the specific data source interface, which is to save the data to the Oracle database*/classOracle_datasourceImplementsData_source { Public functionAdd () {Echo' Oracle Data add '; }     Public functionDelete () {Echo' Oracle data removal '; }     Public functionedit () {Echo' Oracle Data Editor '; }     Public functionfind () {Echo' Oracle data query '; }}/** * File Save method Specific class * A specific data source interface of another form, which is to save the data to a file*/classFile_datasourceImplementsData_source { Public functionAdd () {Echo' File Data add '; }     Public functionDelete () {Echo' File Data deletion '; }     Public functionedit () {Echo' File data edit '; }     Public functionfind () {Echo' File Data lookup '; }}/** * Policy class * This class returns data sources that meet the requirements class on Demand * This example $strategyName = db_source or $strategyName = File_source*/classStrategy { Public Static functionGetstrategy ($data _source) {        return New $data _source (); }}/** * Main Event handling class*/classControl {Private $datasource;  Public function__construct ($datasource) {        $this->datacource = Strategy::getstrategy ($datasource ); }     Public functionAdd () {$this->datacource->Add (); }     Public functionedit () {$this->datacource->edit (); }     Public functionDelete () {$this->datacource->Delete (); }     Public functionfind () {$this->datacource->find (); }}Header(' Content-type:text/html;charset=utf-8 ' );$datasource= "File_datasource";$concretecontrol=NewControl ($datasource );$concretecontrol-Add ();?>

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.