Function description
1. The bridge mode separates the abstract interface and its implementation part to achieve decoupling, which is a better solution than inheritance.
2. Easy to expand. The bridging mode is more flexible than inheritance. It reduces the number of classes created and also facilitates combination.
3. Bridge mode can be used for two independent changing dimensions.
Example
// Employee grouping abstract class Staff { abstract public function staffData(); } class CommonStaff extends Staff { public function staffData() { return "nickname, 小红, 小黑"; } } class VipStaff extends Staff { public function staffData() { return 'Xiaoxing, Xiaolong'; } } // send form //Abstract parent class abstract class SendType { abstract public function send($to, $content); } class QQSend extends SendType { public function __construct() { // Connection method with QQ interface } public function send($to, $content) { return $content. '(To '. $to . ' From QQ)<br>'; } } class SendInfo { protected $_level; protected $_method; public function __construct($level, $method) { // Here you can use a singleton to control resource consumption $this->_level = $level; $this->_method = $method; } public function sending($content) { $staffArr = $this->_level->staffData(); $result = $this->_method->send($staffArr, $content); echo $result; } } //Client call $info = new SendInfo(new VipStaff(), new QQSend()); $info->sending('Go home for dinner'); $info = new SendInfo(new CommonStaff(), new QQSend()); $info->sending('Continue to work'); Output result: Go home for dinner (To Xiaoxing, Xiaolong From QQ) Continue to work (To 小名, 小红, 小黑 From QQ)
The above is the function of PHP bridge mode, I hope it will be helpful to everyone.