<?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 ();?>