Six principles of software design patterns of the five pick Demeter (PHP language)

Reprinted, the original link https://www.cnblogs.com/zhanghengscnc/p/8299459.html

Definition: An object should be kept to a minimum understanding of other objects.

The origin of the problem: the closer the relationship between class and class, the greater the degree of coupling, when a class is changed, the influence of another class is also greater.

Solution: minimize coupling between the class and class.

Since the program began contacting us, we know the general principles of software programming: low coupling, high cohesion. Whether or process-oriented programming object-oriented programming, only the coupling between the individual modules as low as possible, in order to improve the rate of code reuse. The advantages of coupling low self-evident, but how can we do the programming and low coupling it? That is what the Law of Demeter to be done.

Demeter also known as the principle of the least known, was first proposed by Ian Holland America Northeastern University in 1987. Popular speaking, is a class of their dependent classes know better. That is, for the dependent classes, no matter how complex logic, as much as possible to the internal logic is encapsulated in the class, the external addition method provided by the public, without leaking any information outside. Demeter there is a more simple definition: only direct communication with friends. First, let's explain what is direct friends: Each object has a coupling relationships to other objects, as long as the coupling relationship between two objects, we say that friendship between the two objects. Many coupling mode, dependent, associated, combined, polymerization. Among them, we call appearance member variables, method arguments, method return values ​​directly in the class friends, now out of the local variables of the class are not direct friends. In other words, unfamiliar class best not to appear as a form of local variables within the class.

As an example: There is a group company, has branch offices and subordinate units directly under the department, employee ID is now required to print out all subordinate units. First look at the violation of the Law of Demeter Design:

//总公司员工
class Employee
{
    private $id;

    public function setId(String $id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }
}

//分公司员工
class SubEmployee
{
    private $id;

    public function setId(String $id)
    {$this->id = $id; 
    } 

    Public  function getId () 
    { 
        return  $ the this -> ID; 
    } 
} 

class SubCompanyManager 
{ 
    public  function getAllEmployee () 
    { 
        $ List = [];
         for ( $ I = 0; $ I <10; $ I ++ ) {
             EMP $ = new new SubEmployee ();
             // assigned an ID in order for the branch office 
            $ EMP -> the setId ( "branch." $ I );
             $ List [] =EMP $ ; 
        } 
        return  $ List ; 
    } 
} 

class CompanyManager 
{ 
    public  function getAllEmployee () 
    { 
        $ List = [];
         for ( $ I = 0; $ I <10; $ I ++ ) {
             $ EMP = new new the Employee () ;
             // Branch staff assigned to a sequence ID 
            $ EMP -> the setId ( "Corporation." $ I );
             $ List [] = $ EMP ; 
        } 
        return  $ List;
    }

    public function printAllEmployee(SubCompanyManager $sub)
    {
        $list1 = $sub->getAllEmployee();
        foreach($list1 as $v){
            print_r($v->getId() . "<br/>");
        }

        $list2 = $this->getAllEmployee();
        foreach($list2 as $v){
            print_r($v->getId() . "<br/>");
        }
    }
}

$e = new CompanyManager();
$e->printAllEmployee(new SubCompanyManager());

 

Now that the main problem lies in the design CompanyManager in accordance Demeter, occurs only direct communication friends, and friends SubEmployee class is not directly CompanyManager class (coupled to a local variable appears not direct friends), the logic speaking only to his headquarters branch coupling on the line with branch employees did not have any contact, this design clearly adds unnecessary coupling. According to the Law of Demeter, indirect coupling such friendships class appearance should be avoided. The modified code as follows:

 

//总公司员工
class Employee
{
    private $id;

    public function setId(String $id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }
}

//分公司员工
class SubEmployee
{
    private $id;

    public function setId(String $id)
    {
        $this->id = $id; 
    } 

    Public  function getId () 
    { 
        return  $ the this -> ID; 
    } 
} 

class SubCompanyManager 
{ 
    public  function getAllEmployee () 
    { 
        $ List = [];
         for ( $ I = 0; $ I <10; $ I ++ ) {
             EMP $ = new new SubEmployee ();
             // assigned an ID in order for the branch office 
            $ EMP -> the setId ( "branch." $ I );
             $ List [] =$emp;
        }
        return $list;
    }

    public function printEmployee(SubCompanyManager $sub)
    {
        $list = $sub->getAllEmployee();
        foreach($list as $v){
            print_r($v->getId() . "<br/>");
        }
    }
}

class CompanyManager
{
    public function getAllEmployee()
    {
        $list = [];
        for( $ I = 0; $ I <10; $ I ++ ) {
             $ EMP = new new the Employee ();
             // assigned an ID in order for the branch office 
            $ EMP -> the setId ( "Corporation." $ I ) ;
             $ List [] = $ EMP ; 
        } 
        return  $ List ; 
    } 

    public  function printAllEmployee (CompanyManager $ Sub ) 
    { 
        $ List = $ Sub -> getAllEmployee ();
         the foreach ( $ List  AS  $ V){
            print_r($v->getId() . "<br/>");
        }
    }
}

$e = new CompanyManager();
$e->printAllEmployee(new CompanyManager());

$e = new SubCompanyManager();
$e->printEmployee(new SubCompanyManager());

 

After modifying, printing method for the division increased personnel ID, and call the head office to print directly, thus avoiding the coupling with the branch staff.

Demeter intention is to reduce the coupling between classes, each class is reduced due to the unnecessary reliance, therefore it does reduce coupling relationship. But everything has a degree, although to avoid indirect communication with the class, but to communicate, is bound to have contact through a "intermediary", such as in this case, the company is through the branches of this "intermediary" to the branch the staff of the occurrence of contact. Excessive use of the principle of Demeter, a large amount of such transfer and intermediary classes, resulting in the complexity of the system increases. Therefore, when employed to weigh Demeter, do both clear structure, but also the high cohesion and low coupling.

Guess you like

Origin www.cnblogs.com/shamohai/p/11100467.html