PHP coupling design model understanding _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Understanding of PHP coupling design patterns. A software has many classes, and classes need to call each other. Once a class is closely coupled with another class, the reusability of the software will be greatly reduced. A software has many classes, and classes need to call each other. Once a class is closely coupled with another class, the reusability of the software will be greatly reduced. Therefore, the reusability of a software depends on its coupling degree.

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

During the design process, it is proposed that when the architecture of the software is designed, the logic running part (SimpleRouter class) and output part (SimpleRouterFrame class) of the software are discovered) it cannot be well combined. That is, we have to pass the reference to SimpleRouterFrame of the program interface layer by layer to the core part of the program to provide the output function.

During the development process, it is proposed that after some modifications are made to the output interface (SimpleRouterFrame class), especially after some method names are modified, the corresponding program core (SimpleRouter class) the code also needs to be modified to adapt to the new output interface.

Cause of the problem: the coupling between the class and the class is too tight, so that each time you need to modify a class, its associated classes need to modify the code to adapt to the modified class. For example, A Class A needs to call the public method of another class B directly. Once B no longer supports this method or overwrites the method name, A needs to rewrite the code to adapt. Another case: A class A needs Class B with A specific method, but the form of B is not determined. Once the internal structure of B changes, A may need to rewrite the code.

To avoid this situation, we need to reduce the coupling between A and B. regardless of the form, as long as B can still implement the functions required by A, A does not need to rewrite the code. solution: let B implement some interface I and define I. method (); at the same time, A can directly call the I Method when calling B's Method; in the past, B will be passed as A parameter to A, and then A will call B's Method.

{       A.AMethod( B b ) {       b.BMethod();       /*….*/     }   }   

Modify:

{       A.AMethod( I i ) {       i.Method();   }   }   

Here, B only needs to implement the I. Method () Method, completely hiding the implementation details. This method not only achieves loose coupling between classes, but also greatly enhances the reusability of classes. Looking back at the previously learned design patterns, we can find that this is similar to the Observer pattern.

The following is a complete example:

 Varl = new LazyDog () ;}function compute ($ a, $ B) {return $ this-> varl-> say ();}} /* it can also be implemented in an inherited way: class Coupling extends LazyDog implements Calculation {function compute ($ a, $ B) {return parent: say ();}} */class LazyDog {function say () {return "I do not perform any operations... just to implement the 'coupling design mode '... I made soy sauce ...... ";}} class Test {private $ one; private $ two; public function _ construct ($ x, $ y) {$ this-> one = $ x; $ this-> two = $ y; echo "Class Test initialization: attribute \ $ one = ". $ this-> one. ", attribute \ $ two = ". $ this-> two."
 ";} Function display (Calculation $ a) {return" operations implemented using the PHP interface technology :". $ a-> compute ($ this-> one, $ this-> two )."
 ";}}$ T = new Test (96,12); $ t1 = new Addition (); $ t2 = new Subtraction (); $ t3 = new Multiplication (); $ t4 = new Division (); $ t5 = new Modf (); $ dog = new Coupling (); echo $ t-> display ($ t1 ); echo $ t-> display ($ t2); echo $ t-> display ($ t3); echo $ t-> display ($ t4 ); echo $ t-> display ($ t5); echo $ t-> display ($ dog);?>

Program running result:

Class Test initialization: attribute $ one = 96, attribute $ two = 12 calculation implemented using PHP interface technology: addition calculation result: 108 calculation implemented using PHP interface technology: the result of the subtraction operation is 84. The operation is implemented using the PHP interface technology. the result of the multiplication operation is 1152. The operation is implemented using the PHP interface technology. the result of the division operation is: 8. operations implemented using PHP interface technology: the result of the modulo operation is: 0. operations implemented using PHP interface technology: I do not perform any operations... just to implement the 'coupling design mode '... I made soy sauce ......

....

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.