>  기사  >  백엔드 개발  >  (5) 객체 지향 설계 원칙 1---일반 개요 및 단일 책임 원칙

(5) 객체 지향 설계 원칙 1---일반 개요 및 단일 책임 원칙

WBOY
WBOY원래의
2016-08-10 08:48:40935검색

1. 일반 개요:

1. 객체지향의 5가지 원칙: 단일 책임 원칙, 인터페이스 격리 원칙, 개방형 폐쇄 원칙, 대체 원칙, 종속성 반전 원칙.

2. 단일 책임 원칙:

1. 클래스에 관한 한 변경 이유는 단 하나, 단일 책임 원칙입니다.

2. 단일 책임에는 두 가지 의미가 있습니다.

a. 동일한 책임을 다른 클래스에 분산시키지 마십시오.

b. 하나의 클래스에 너무 많은 책임을 맡기지 마십시오.

3. 책임 원칙의 이유: 클래스 간의 결합을 줄이고 클래스의 재사용성을 향상시킵니다.

3. 팩토리 패턴:

1. 팩토리 패턴을 사용하면 코드가 실행될 때 개체가 인스턴스화될 수 있습니다. 물체를 '생산'할 수 있습니다.

2. 예:

<?php
/*
 * 单一职责原则
 */
interface Db_Adapter{
    /*
     * 数据库连接
     * @param $config数据库配置
     * @return resource
     */
    public function connect($config);
    /*
     * 执行数据库查询
     * @param string $query 数据库查询sql字符串
     * @param mixed $handld 连接对象
     * @return resource
     */
    public function query($query,$handle);
}

class Db_Adapter_Mysql implements Db_Adapter{
    private $_dbLink;
    /*
     * 数据库连接
     *
     * @param $config 数据库配置
     * @throws Db_Exception
     * @return resource
     */
    public function connect($config){
        if ($this->_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;
            }
        }
        /*数据库异常*/
        throw new Db_Exception(@mysql_error($this->_dbLink)); //这一句报了很多错
    }

    /*
     * 执行数据库查询
     *
     * @param string $query 数据库查询sql字符串
     * @param mixed $handle 连接对象
     * @return resource
     */
    public function query($query,$handle){
        $resource = "";
        if ($resource == @mysql_query($query,$handle)){
            return $resource;
        }
    }
}

class Db_Adapter_sqlite implements  Db_Adapter{
    private $_dbLink; //数据库连接字段标记
    /*
     * 数据库连接函数
     *
     * @param $config数据库配置
     * @throws DB_exception
     * @return resource
     */
    public function connect($config){
        if ($this->_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;
            }
        }
        /*数据库异常*/
        throw new Db_exception(@mysql_error($this->dbLink));
    }

    /*
     * 执行数据库查询
     *
     * @param string $query 数据库查询sql字符串
     * @param mixed $handle 连接对象
     * @return resource
     */
    public function query($query,$handle){
        $resource = "";
        if ($resource == @mysql_query($query,$handle)){
            return $resource;
        }
    }
}

$testDb = new Db_Adapter_Mysql();
$config = array(
    //这里写数据库配置
    'host'=>'localhost',
);

$testDb->connect($config);
$testDb->query($sql,$handle);

저작권 표시: 이 글은 블로거의 원본 글이므로 블로거의 허락 없이 복제할 수 없습니다.

위에서는 (5) 객체 지향 설계 원칙 1---콘텐츠 측면을 포함한 일반적인 개요와 단일 책임 원칙을 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.