This article introduces the content is about PHP design mode bridge mode, has a certain reference value, now share to everyone, the need for friends can refer to
Bridging Mode (bridge) is an object-structured pattern that separates the abstraction from the implementation and makes it possible to change independently.
in summary, it is in the multi-dimensional, the changes do not affect each other, through bridging to establish a certain association, dynamic combination, this mode of flexibility is relatively high.
Like we go to eat rice bowls in the same as the vegetables, such as green pepper pork cover rice, potato and beef covered with food.
Staple food: rice, noodles.
Complementary food: shredded pork with green pepper and beef with potatoes.
Staple food, complementary food two different dimensions, each can continue to add kinds, for example: Complementary food can be added another leek eggs, and can be combined with each other.
The recording code is as follows:
/** abstract a staple class * abstract food */abstract class Foods { Public $dishes; The object is initially assigned an abstract function makefood (); }/** rice bowls inherit staple class * rice */class Rice extends Food {function Makefood () {$this ->dishes->makedishes (); echo "Rice bowls <br/>"; }}/** cover pouring surface class inherit staple class * Noodle */class Noodle extends Food {function Makefood () { $this->dishes->makedishes (); echo "Cover pouring surface <br/>"; }}/** Dishes Interface * Interface Dishes */interface Dishes {function makedishes (); }/** Green pepper shredded meat class inherit dishes interface * QJRS */class Qjrs implements dishes {function makedishes () {echo ' green pepper Shredded pork "; }}/** potato Beef class inherit dishes interface * TDNR */class TDNR implements dishes {function makedishes () {E Cho "Potato beef"; } }
<?php //bridge mode index.php header ("Content-type:text/html;charset=utf-8"); Require_once "bridge.php"; To a rice bowl $rice = new Rice (); The toppings to pepper shredded pork $rice->dishes = new Qjrs (); Serving $rice->makefood (); The same to a rice bowls $rice = new Rice (); This time changed the toppings to Potato beef $rice->dishes = new Tdnr (); Serving $rice->makefood ();
Output Result:
Shredded Pork with green pepper
Potato and beef rice bowls
Related recommendations:
PHP design mode Adapter mode
The builder mode of PHP design mode
Prototype mode of PHP design mode