: This article mainly introduces (5) Object-Oriented Design Principles 1 --- general principles and single responsibility principles. if you are interested in PHP tutorials, refer to them. I. general principles:
1. five object-oriented principles: single responsibility principle, interface isolation principle, open-closed principle, replacement principle, and dependency inversion principle.
II. single responsibility principle:
1. for a class, there is only one reason for its change: single responsibility principle.
2. a single responsibility has two meanings:
A. avoid spreading the same responsibilities to different classes.
B. avoid a class from assuming too many responsibilities
3. reasons for following the single responsibility principle: reduce coupling between classes and improve reusability of classes.
III. factory model:
1. the factory mode allows object instantiation during code execution. Can 'product' object.
2. example:
_ DbLink = @ mysql_connect ($ config-> host. (empty ($ config-> port )? '':':'. $ Config-> port), $ config-> user, $ config-> password, true) {if (@ mysql_select_db ($ config-> database, $ this-> _ dbLink) {if ($ config-> charset) {mysql_query ("set names '{$ config-> charset }'", $ this-> _ dbLink);} return $ this-> _ dbLink ;}} /* database exception */throw new Db_Exception (@ mysql_error ($ this-> _ dbLink )); // This sentence reports many errors}/** execute database query ** @ param string $ query database query SQL string * @ param mixed $ handle connection object * @ retur N resource */public function query ($ query, $ handle) {$ resource = ""; if ($ resource = @ mysql_query ($ query, $ handle )) {return $ resource ;}} class Db_Adapter_sqlite implements Db_Adapter {private $ _ dbLink; // Mark the database connection field/** database connection function ** @ param $ config database configuration * @ throws DB_exception * @ return resource */public function connect ($ config) {if ($ this-> _ dbLink = @ mysql_connect ($ config-> host. (empty ($ config-> p Ort )? '':':'. $ Config-> port), $ config-> user, $ config-> password, true) {if (@ mysql_select_db ($ config-> database, $ this-> _ dbLink) {if ($ config-> charset) {mysql_query ("set names '{$ config-> charset }'", $ this-> _ dbLink);} return $ this-> _ dbLink ;}} /* database exception */throw new Db_exception (@ mysql_error ($ this-> dbLink ));} /** execute database query ** @ param string $ query database query SQL string * @ param mixed $ handle connection object * @ return resource */public function query ($ query, $ handle) {$ resource = ""; if ($ resource ==@ mysql_query ($ query, $ handle) {return $ resource ;}}} $ testDb = new Db_Adapter_Mysql (); $ config = array (// write the database configuration 'host' => 'localhost',); $ testDb-> connect ($ config ); $ testDb-> query ($ SQL, $ handle );
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces (5) Object-Oriented Design Principles 1 --- general principles and single responsibility principles, including content, and hope to help those who are interested in PHP tutorials.