Home>Article>Backend Development> Let’s take a look at the adapter pattern of PHP design patterns
If you need to When traveling and playing abroad, there are now a lot of electronic products, but every electronic product needs a power source to drive it, such as mobile phones, computers, and cameras. . . But there is another very obvious problem. Electronic products consume a lot of power. When the power is gone, they need to be recharged. In our country, the power chargers that come with the electronic products we buy are generally 220V. , but the world is very big, and the requirements (regulations)
of each country are different. For example, the United States is 120V
1. Class adapter
According to the UML diagram above, we can see that, Requires a US voltage interfaceUsaInterface, an adapter class
Adapter, a Chinese voltage interface
Chine, a user
Computer,
Here, the Chinese voltage is the adaptee, and the American voltage is the final output (target), which is the configurer
interface UsaInterface{ public function outPut5V();}class China{ private $voltage = 220; public function outPut220V() { return $this->voltage; }}class Adapter extends China implements UsaInterface{ public function outPut5V() { // TODO: Implement outPut5V() method. $chinaVoltage = $this->outPut220V(); return $chinaVoltage / 2 + 10 . "V"; }}class Computer{ public function index(UsaInterface $usa) { if ($usa->outPut5V() === "120V") { printf("可以在美国使用中国的电源插头给电脑充电了"); } else { printf("不可以在美国使用中国的电源插头给电脑充电了"); } }}class Client{ public function __construct() { $adapter = new Adapter(); $computer = new Computer(); $computer->index($adapter); }}
2. Object Adapter Mode
Object adapter is actually an adjustment on the class adapter model, so that the adapter classAdapterdoes not inherit the adapted class, but allows them to be combined together, so that the use can be increased The degree of flexibility will not expose many methods of the adaptee (use association relationships instead of inheritance relationships [synthetic reuse principle]). At the same time, without inheritance, there is no way to override the method rewriting of the parent class, which can be considered a good thing and a bad thing. As follows:
class Adapter implements UsaInterface{ protected $china; public function __construct(China $china) { $this->china = $china; } public function outPut5V() { // TODO: Implement outPut5V() method. $chinaVoltage = $this->china->outPut220V(); return $chinaVoltage / 2 + 10 . "V"; }}
When you do not need to implement all the methods provided by the interface, you can first design an abstract class to implement the interface , and provides a default empty method implementation for the interface, then changing the sub-method of the abstract class can selectively override certain methods of the parent class to meet the requirements (applicable to situations where an interface does not want to use all methods )
Related learning recommendations:
The above is the detailed content of Let’s take a look at the adapter pattern of PHP design patterns. For more information, please follow other related articles on the PHP Chinese website!