Coupled design pattern in PHP
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 <?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 "我什么运算都不做...只是为了实现'耦合设计模式'...我是出来打酱油的......";
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 ?>程序运行结果:
1 Class Test 初始化:属性$one=96,属性$two=12 2 用PHP接口技术实现的运算:加法运算结果为:108 3 用PHP接口技术实现的运算:减法运算结果为:84 4 用PHP接口技术实现的运算:乘法运算结果为:1152 5 用PHP接口技术实现的运算:除法运算结果为:8 6 用PHP接口技术实现的运算:取模运算结果为:0 7 用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!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian
Dec 24, 2024 pm 04:42 PM
PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati
How To Set Up Visual Studio Code (VS Code) for PHP Development
Dec 20, 2024 am 11:31 AM
Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
7 PHP Functions I Regret I Didn't Know Before
Nov 13, 2024 am 09:42 AM
If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op
How do you parse and process HTML/XML in PHP?
Feb 07, 2025 am 11:57 AM
This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
Explain JSON Web Tokens (JWT) and their use case in PHP APIs.
Apr 05, 2025 am 12:04 AM
JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
PHP Program to Count Vowels in a String
Feb 07, 2025 pm 12:12 PM
A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total
Explain late static binding in PHP (static::).
Apr 03, 2025 am 12:04 AM
Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases?
Apr 03, 2025 am 12:03 AM
What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.


