Home > Article > Backend Development > Detailed explanation of the proxy pattern of PHP design patterns
In software development, there is also a design pattern that can provide similar functions to purchasing websites. For some reasons, the client does not want or cannot directly access an object. At this time, indirect access can be achieved through a third party called a "proxy". The corresponding design pattern of this solution is called proxy mode
Provides a proxy or placeholder for a certain object, and the proxy object controls the original object Access
<?php /* * 代理模式 */ //代理抽象接口 interface shop { public function buy($title); } //原来的CD商店,被代理对象 class CDShop implements shop { public function buy($title) { echo "购买成功,这是你的《{$title}》唱片" . PHP_EOL; } } //CD代理 class Proxy implements shop { public function buy($title) { $this->go(); $CDshop = new CDshop; $CDshop->buy($title); } public function go() { echo "跑去香港代购" . PHP_EOL; } } class client { public static function shoping($goods) { $proxy = new Proxy; $proxy->buy($goods); } } //许多年后你想买张 醒着做梦 找不到CD商店了,和做梦似的,不得不找了个代理去香港帮你代购。 client::shoping("醒着做梦");
The proxy pattern is one of the commonly used structural design patterns. It provides a solution for indirect access to objects and can access objects For control, the design mode is suitable for people with experience or good foundation to see
Related recommendations:
PHP proxy mode sample code sharing
The above is the detailed content of Detailed explanation of the proxy pattern of PHP design patterns. For more information, please follow other related articles on the PHP Chinese website!