Factory mode
/** * Factory method or class to generate the object, rather than in the code directly new * * Modify the class name, do not need to modify each instantiation statement * Only need to modify the corresponding factory method * * Class Factory * @package Regina */cla SS factory{ static function CreateDatabase () { $db = database::getinstance (); return $db; }}
Single-Case mode
/** * Singleton mode, save resources, a class can only instantiate an object * * Make an object of a class only allowed to create one * * in order to implement Singleton mode, to privatize constructors and clone Magic method * Class Database * @package Regina */class database{ protected static $db; Private Function __construct () { } Private Function __clone () { } static function getinstance () { if (self:: $db) { return to self:: $db; } Self:: $db = new Database (); Return self:: $db; }}
Registration mode
/** * Save the new object in a static value for global sharing * global share and Swap objects * Class Register * @package regina */class register{ protected static $arra y; static function set ($name, $OBJ) { echo ' Register set run ... '; Self:: $array [$name] = $OBJ; } static function Get ($name) { echo ' Register get run ... '; Return self:: $array [$name]; } function _unset ($name) { echo ' Register unset run ... '; Unset (self:: $array [$name]);} }
Adapter mode
/** * Encapsulates a variety of distinct function interfaces into a unified API * Interface Idatabase * @package regina */interface idatabase{ function Connect ($host, $US Ername, $password, $dbname); function query (); function close ();} Class Mysqli implements database{ protected $conn; Function Connect ($host, $username, $password, $dbname) { echo ' mysqli connect run ... '; } function query () { echo ' mysqli query run ... '; } function Close () { echo ' mysqli close run ... '; }} Class Mysql implements database{ function connect ($host, $username, $password, $dbname) { echo ' MySQL Connect Run ... '; } function query () { echo ' Mysql query run ... '; } function Close () { echo ' Mysql close run ... '; }}
Policy mode
Encapsulates a specific set of behaviors and algorithms into classes to accommodate the definition of a dictation context.
Dig pits to fill~~~~~
php-Common Design Patterns