PHP's Coupling design pattern

Source: Internet
Author: User
This article mainly introduces the implementation of PHP coupling design mode, has a certain reference value, now share to everyone, the need for friends can refer to

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

1    {      2        a.amethod (b b) {      3            b.bmethod ();      4            /*....*/      5        }      6    }

modified to:

1 {2 A.amethod (I i) {3 i.method (); 4} 5} 

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.

below is a A complete example:



001 <?php 002 Interface Calculation {003 function compute ($a, $b);        004} 005 006 Class addition implements calculation {007 function compute ($a, $b) 008 {009 Return "The result of the addition operation is:".    ($a + $b); 010} 011} 012 013 Class Subtraction Implements calculation {014 function compute ($ A, $b) 015 {016 Return "subtraction operation result is:". (    $a-$b); 017} 018} 019 020 class multiplication Implements calculation {021 function comput E ($a, $b) 022 {023 return "The result of the multiplication operation is:". (    $a * $b);     024} 025} 026 027 Class Pision implements calculation{028 function compute ($a, $b) The result of the 029 {030 Return "division operation is:". (    $a/$b);    031} 032} 033 034 class MODF implements calculation {035 function compute ($a, $b) 036 {037 REturn "Modulo operation result is:".    ($a% $b);  038} 039} 040 041 class coupling implements calculation {042//Direct here: public $varl = New Lazydog ();    Will go wrong.    043 public $varl = null;    044 045 function __construct () 046 {047 $this->varl = new Lazydog (); 048} 049 050 function compute ($a, $b) 051 {052 return $this->var    L->say (); 053} 054} 055 056/* can also be implemented in an inherited way yo: 057 class Coupling extends Lazydog implements Calcula    tion {058 function compute ($a, $b) 059 {060 return Parent::say ();    061} 062} 063 */064 065 class Lazydog {066 function say () 067 { 068 return "I don't do any arithmetic ... Just to achieve the ' coupling design pattern ' ...    I came out to soy sauce ... ";    069} 070} 071 072 class Test {073 private $one; 074 Private $two;    075 Public Function __construct ($x, $y) 076 {077 $this->one= $x;    078 $this->two= $y; 079 echo "Class Test initialization: properties \ $one =". $this->one. ", property \ $two =". $this->two. "    

program run Result:


1 class Test initialization: attribute $one=96, attribute $two=12 2 operations implemented with PHP interface technology: The result of the addition operation is: 108 3 with PH    The operation of the P-interface technology: The subtraction operation results in: 84 4 operations implemented with PHP interface technology: The multiplication result is: 1152 5 operations implemented with PHP interface technology: The result of Division is: 8 6 operation using PHP interface technology: The result of modulo operation is: 0 7 operation with PHP interface technology: I don't do any arithmetic ... Just to achieve the ' coupling design pattern ' ... I came out to 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.