Home  >  Article  >  Backend Development  >  Coupled design pattern in PHP

Coupled design pattern in PHP

不言
不言Original
2018-04-28 13:42:462002browse

This article mainly introduces the coupling design pattern for implementing PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

A software that has many Classes need to call each other. Once a class has a tightly coupled relationship with another class, the reusability of the software will be greatly reduced. Therefore, the reusability of a software depends on its degree of coupling.

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

During the design process, it was proposed: When designing the architecture of this software, it was discovered that the logical operation part (SimpleRouter class) and the output part (SimpleRouterFrame class) of this software cannot be well combined. That is: we have to pass the reference to the program interface (reference to SimpleRouterFrame) layer by layer to the core part of the program to provide the output function.

During the development process, it was proposed: When we make some modifications to the output interface (SimpleRouterFrame class), especially after the names of some methods are modified, the code in the core part of the corresponding program (SimpleRouter class) also needs to be modified. to adapt to this new output interface.

Cause of the problem: The coupling between classes is too tight, so that every time a class needs to be modified, its corresponding associated class needs to modify the code to adapt to the modified class. For example: a certain class A needs to directly call the public method of another class B. Once B no longer supports this method, or rewrites the method name, A will need to rewrite the code to adapt. Another situation: A certain class A needs to use a class B with a specific method, but the form of B is uncertain. Once the internal structure of B changes, A may need to rewrite the code.

To avoid this situation, it is necessary to reduce the coupling between A and B. Regardless of the form, as long as B can still achieve the functions required by A, A does not need to rewrite the code. Solution: Let B implements a certain interface I and defines I.Method(); at the same time, A can directly call I's method when calling B's method; in the past, B would be passed to A as a parameter, and then A would call B's method. Place

1    {      
2        A.AMethod(B b ) {      
3            b.BMethod();      
4            /*….*/      
5        }      
6    }

is 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. According to this method, loose coupling between classes is achieved and the reusability of classes is greatly enhanced. Looking back at the design patterns we have learned before, we can find that they are similar to the Observer pattern.

下面是一个完整的例子:



001    varl = new LazyDog();    
048        }    
049             
050        function compute($a, $b)    
051        {    
052            return $this->varl->say();    
053        }    
054    }    
055         
056    /*也可以用继承的方式实现哟:    
057    class Coupling extends LazyDog implements Calculation {    
058        function compute($a, $b)    
059        {    
060            return parent::say();    
061        }    
062    }    
063    */    
064         
065    class LazyDog {    
066        function say()    
067        {    
068            return "我什么运算都不做...只是为了实现'耦合设计模式'...我是出来打酱油的......";    
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 初始化:属性\$one=".$this->one.",属性\$two=".$this->two."";    
080       }    
081       function display(Calculation $a){    
082            return "用PHP接口技术实现的运算:".$a->compute($this->one,$this->two)."
"; 083 } 084 } 085 086 $t = new Test(96,12); 087 $t1 = new Addition(); 088 $t2 = new Subtraction(); 089 $t3 = new Multiplication(); 090 $t4 = new pision(); 091 $t5 = new Modf(); 092 093 $dog = new Coupling(); 094 095 echo $t->display($t1); 096 echo $t->display($t2); 097 echo $t->display($t3); 098 echo $t->display($t4); 099 echo $t->display($t5); 100 101 echo $t->display($dog); 102 103 ?>

程序运行结果:


1    Class Test 初始化:属性$one=96,属性$two=12    
2    用PHP接口技术实现的运算:加法运算结果为:108    
3    用PHP接口技术实现的运算:减法运算结果为:84    
4    用PHP接口技术实现的运算:乘法运算结果为:1152    
5    用PHP接口技术实现的运算:除法运算结果为:8    
6    用PHP接口技术实现的运算:取模运算结果为:0    
7    用PHP接口技术实现的运算:我什么运算都不做...只是为了实现'耦合设计模式'...我是出来打酱油的......

相关推荐:

PHP的标准库


The above is the detailed content of Coupled design pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn