Factory mode
The most mentioned, the use is also the most extensive. Simply put: Define an interface for user-created objects.
Simple Factory mode:
① abstract base class: Some methods of defining abstractions in classes to implement in subclasses
② inherits from the subclass of the abstract base class: Implementing an abstract method in a base class
③ Factory class: Used to instantiate objects
As long as the different class instances can be generated according to different parameters, then it conforms to the design idea of the Factory mode.
This makes it easy to expand. Instead of using code new Mysql ($host, $username, $password, $dbname) in the direct link database, we can dynamically generate an instance of a connected database. It can be MySQL, or it can be connected to Oracle. Interface DB { function connect ();} Interface Factory { function createdb ();} Class MySQLdb implements DB { public function Connect () { echo ' connected to MySQL '; }} Class SQLite implements DB { public function connect () { echo ' connected sqllist '; }} Class Createmysql implements Factory {public function createdb () { return new mysqldb (); }} Class Crearelite implements Factory {public function createdb () { return new SQLite () }} $fact = new creat Emysql (); Var_dump ($fact->createdb ()->connect ());
This one's good.
http://blog.csdn.net/hguisu/article/details/7505909
A brief analysis of PHP design pattern