The bridging mode of PHP design mode

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.