Coupled design pattern in PHP

不言
Release: 2023-03-25 06:40:01
Original
2136 people have browsed it

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    }
Copy after login

is modified to:

1    {      
2        A.AMethod(I i ) {      
3            i.Method();      
4        }      
5    }
Copy after login

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    <?php    
002    interface Calculation {    
003        function compute($a, $b);    
004    }    
005         
006    class Addition implements Calculation {    
007        function compute($a, $b)    
008        {    
009            return "加法运算结果为:".($a+$b);    
010        }    
011    }    
012         
013    class Subtraction implements Calculation {    
014        function compute($a, $b)    
015        {    
016            return "减法运算结果为:".($a-$b);    
017        }    
018    }    
019         
020    class Multiplication implements Calculation {    
021        function compute($a, $b)    
022        {    
023            return "乘法运算结果为:".($a*$b);    
024        }    
025    }    
026         
027    class pision implements Calculation{    
028        function compute($a, $b)    
029        {    
030            return "除法运算结果为:".($a/$b);    
031        }    
032    }    
033         
034    class Modf implements Calculation {    
035        function compute($a, $b)    
036        {    
037            return "取模运算结果为:".($a % $b);    
038        }    
039    }    
040         
041    class Coupling implements Calculation {    
042        //这里直接:public $varl = new LazyDog(); 会出错。    
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->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 "我什么运算都不做...只是为了实现&#39;耦合设计模式&#39;...我是出来打酱油的......";    
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."<h />";    
080       }    
081       function display(Calculation $a){    
082            return "用PHP接口技术实现的运算:".$a->compute($this->one,$this->two)."<hr/>";    
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    ?>
Copy after login

程序运行结果:


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

相关推荐:

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template