This article mainly introduces the detailed explanation and cases of PHP bridge mode. Interested friends can refer to it. I hope it will be helpful to everyone.
Bridge mode (Bridge) is an object structural mode that separates the abstract part from the implementation part so that they can change independently.
In summary, in multiple dimensions, their respective changes do not affect each other. Certain associations are established through bridging and dynamic combination is performed. This model has relatively high flexibility.
It’s just like when we eat rice bowl with various dishes, such as green pepper and shredded pork rice bowl, potato and beef rice bowl.
Staple food: rice, noodles.
Supplementary food: shredded pork with green pepper, potatoes and beef.
Staple food and complementary food are two different dimensions, and each can continue to add types. For example: complementary food can add another leek, egg, etc., and can be combined with each other.
The recording code is as follows:
/** * Created by Sublime. * User: Ryan * Date: 2018/1/22 * File:Bridge.php */ /** 抽象一个主食类 * abstract Food */ abstract Class Food { public $dishes; // 一开始会赋值对象 abstract function MakeFood(); } /** 盖浇饭类 继承主食类 * Rice */ Class Rice extends Food { function MakeFood() { $this->dishes->MakeDishes(); echo "盖浇饭<br/>"; } } /** 盖浇面类 继承主食类 * Noodle */ Class Noodle extends Food { function MakeFood() { $this->dishes->MakeDishes(); echo "盖浇面<br/>"; } } /** 菜肴接口 * interface Dishes */ interface Dishes { function MakeDishes(); } /** 青椒肉丝类 继承菜肴接口 * QJRS */ Class QJRS implements Dishes { function MakeDishes(){ echo "青椒肉丝"; } } /** 土豆牛肉类 继承菜肴接口 * TDNR */ Class TDNR implements Dishes { function MakeDishes(){ echo "土豆牛肉"; } }
<?php // 桥接模式 index.php header("Content-Type:text/html;charset=utf-8"); require_once "Bridge.php"; // 要一份盖浇饭 $rice = new Rice(); // 浇头要 青椒肉丝 $rice->dishes = new QJRS(); // 上菜 $rice->MakeFood(); // 同样的要一份盖浇饭 $rice = new Rice(); // 这次改了浇头要 土豆牛肉 $rice->dishes = new TDNR(); // 上菜 $rice->MakeFood();
Output result:
Green pepper shredded pork rice bowl
Potato beef rice bowl
Related Recommended:
PHP Design Patterns Bridge Mode
JavaScript Design Pattern Series Six: Bridge Mode
linux How to configure static IP in bridge mode
The above is the detailed content of Detailed explanation and cases of PHP bridge mode. For more information, please follow other related articles on the PHP Chinese website!