PHP Coupling Design Pattern Understanding _php Tutorial

Source: Internet
Author: User
A software that has many classes that need to be called between classes and classes, and that once a class has a tightly coupled relationship with another class, the reusability of the software is greatly reduced. Therefore, the reusability of a software depends on the degree of coupling between the high and low.

Coupling degree: the degree of association and dependence between program modules.

During the design process, it is suggested that when designing the architecture of this software, it is found that the logic running part (Simplerouter Class) and output part (Simplerouterframe Class) of this software cannot be combined well. That is: we have to pass the reference to the Program interface (reference to simplerouterframe) one layer at a to the core of the program to provide output functionality.

In the development process: when we make some changes to the output interface (Simplerouterframe Class), especially after some method name modification, the code of the corresponding program core (Simplerouter Class) also needs to be modified to adapt to the new output interface.

The cause of the problem: the coupling between classes and classes is so tight that each time a class needs to be modified, its associated classes need to modify the code to accommodate the modified class. For example, a class A needs to directly display a call to another class B public method, once b no longer support this method, or override the method name, a will need to rewrite the code to adapt. Another scenario: A Class A needs to use class B with a particular method, but the form of B is not certain, and once the internal structure of B is changed, a may need to rewrite the code.

To avoid this situation, you need to reduce the coupling between A and B, regardless of the form, as long as B can still achieve the function required by a, a does not need to rewrite the code, the solution: B to implement a certain interface I, the definition of I.method (); At the same time a method of invoking the method of the B can be called directly, and the former will pass B as a parameter to a, and then a again call the method of B where

{       A.amethod (b b) {       b.bmethod ();       /*....*/     }   }   

Modified to:

{       A.amethod (i i) {       i.method ();   }   }   

Here, b only needs to implement the I.method () method, completely hiding the implementation details. In this way, the loose coupling between classes and classes is realized, which greatly enhances the reusability of classes. Looking back at the design patterns you've learned in the past, you can see that this is similar to the Observer model.

The following is a complete example:

 Varl = new Lazydog (); }function compute ($a, $b) {return $this->varl->say ();}} /* can also be implemented in an inherited way yo: class Coupling extends Lazydog implements calculation {function compute ($a, $b) {return Parent::say ();}} */class Lazydog {function say () {return "I don't do any arithmetic ... Just to achieve the ' coupling design pattern ' ... I came out to soy sauce ... ";}}   Class Test {private $one;p rivate $two;p ublic function __construct ($x, $y) {$this->one= $x; $this->two= $y; echo "Class Test initialization: properties \ $one =". $this->one. ", properties \ $two =". $this->two. "
 
  
 "; The function display (calculation $a) {return "Operation implemented with PHP interface technology:". $a->compute ($this->one, $this->two). "
 
  
 "; }} $t = new Test (96,12), $t 1 = new addition (), $t 2 = new subtraction (), $t 3 = new multiplication (), $t 4 = new Division (); $t 5 = New Modf (), $dog = new coupling (), Echo $t->display ($t 1), Echo $t->display ($t 2), Echo $t->display ($t 3); Echo $t- >display ($t 4), Echo $t->display ($t 5), Echo $t->display ($dog); >

Program Run Result:

Class Test Initialization: attribute $one=96, attribute $two=12 operation implemented with PHP interface technology: The result of the addition operation is: 108 operation using PHP interface technology: The result of the subtraction operation is: 84 operation using PHP interface technology: The result of the multiplication operation is : 1152 operation with PHP interface technology: The result of the division operation is: 8 operation using PHP interface technology: The result of the modulo operation is: 0 operation using PHP interface technology: I do not do any operations ... Just to achieve the ' coupling design pattern ' ... I came out to soy sauce ...

http://www.bkjia.com/PHPjc/752374.html www.bkjia.com true http://www.bkjia.com/PHPjc/752374.html techarticle a software that has many classes that need to be called between classes and classes, and that once a class has a tightly coupled relationship with another class, the reusability of the software is greatly reduced. ...

  • Related Article

    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.