PHP design pattern bridge mode

不言
Release: 2023-03-24 10:02:02
Original
1697 people have browsed it

This article introduces the bridge mode of PHP design pattern, which has certain reference value. Now I share it with you. Friends in need can refer to it

Bridge mode (Bridge) is an object structural pattern that separates the abstract part from the implementation part so that they can change independently.
To sum up, in multiple dimensions, their respective changes do not affect each other, and a certain association is established through bridging for dynamic combination. Comparison of the flexibility of this model high.

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:

       
    /** 抽象一个主食类      
 *  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 "土豆牛肉";  
   }      
}
Copy after login
<?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();
Copy after login


Output result:

Green pepper shredded pork rice bowl
Potato Beef Rice Bowl

Related recommendations:

PHP Design Pattern Adapter Pattern

Builder Pattern of PHP Design Pattern

Prototype Pattern of PHP Design Pattern


The above is the detailed content of PHP design pattern bridge mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!