(5) Object-Oriented Design Principles 1 --- general principles and single responsibility principles

Source: Internet
Author: User
: 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.

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.