> **設計模式-策略模式**
## 說明 ##
**橋接模式 (Bridge Pattern):將抽象與實現解耦,使得兩者可以獨立的變化**
----------
##模式說明##
在軟件系統中,某些類型由于自身的邏輯,它具有兩個或多個維度的變化,橋接模式就是應對這種多維度的變化
舉例說明:人使用手機看視頻 此時就有三個維度,什么類型的人、什么品牌的手機、什么類型的視頻
抽象出人,手機,視頻,分別實現。其中人作為最底層,使用手機看視頻。
##代碼示例##
<?php
/**
* 橋接模式 (Bridge Pattern):將抽象與實現解耦,使得兩者可以獨立的變化
*/
//在軟件系統中,某些類型由于自身的邏輯,它具有兩個或多個維度的變化,橋接模式就是應對這種多維度的變化
//舉例說明:人使用手機看視頻 此時就有三個維度,什么類型的人、什么品牌的手機、什么類型的視頻
//抽象出人,手機,視頻,分別實現。其中人作為最底層,使用手機看視頻。
/**
* Interface Mobile 手機接口
*/
interface Mobile{
function mType();
}
/**
* Class xiaomi 小米手機
*/
class xiaomi implements Mobile{
function mType()
{
echo "小米手機";
}
}
/**
* Class huwei 華為手機
*/
class huwei implements Mobile{
function mType()
{
echo "華為手機";
}
}
/**
* Interface Video 視頻類型接口
*/
interface Video {
function vType();
}
/**
* Class GameVideo 游戲視頻類
*/
class GameVideo implements Video{
function vType()
{
echo "觀看游戲視頻";
}
}
/**
* Class LearnVideo 學習視頻類
*/
class LearnVideo implements Video{
function vType()
{
echo "觀看學習視頻";
}
}
/**
* Interface People 人群接口
*/
interface People{
function uses();
}
/**
* Class Students 學生類
*/
class Students implements People{
public $mobile;
public $video;
public function __construct($mobile,$video){
$this->mobile = $mobile;
$this->video = $video;
}
function uses()
{
echo "學生會使用";
$this->mobile->mType();
$this->video->vType();
}
}
/**
* Class Teaches 老師類
*/
class Teaches implements People{
public $mobile;
public $video;
public function __construct($mobile,$video){
$this->mobile = $mobile;
$this->video = $video;
}
function uses()
{
echo "老師會使用";
$this->mobile->mType();
$this->video->vType();
}
}
/**
* Class Client 客戶端連接 進行調用
*/
class Client{
public static function main(){
$mobile = new xiaomi();
$video = new LearnVideo();
$road = new Students($mobile,$video);
$road->uses();
}
}
Client::main();
----------
結果:學生會使用小米手機觀看學習視頻
## 結尾 ##
<p style="background-image: -webkit-linear-gradient(left, #3498db, #f47920 10%, #d71345 20%, #f7acbc 30%,#ffd400 40%, #3498db 50%, #f47920 60%, #d71345 70%, #f7acbc 80%, #ffd400 90%, #3498db);color: transparent;-webkit-text-fill-color: transparent;-webkit-background-clip: text;text-align:center;">
心如花木,向陽而生。
</p>
[1]: https://blog.zxliu.cn/usr/uploads/2020/11/2092914566.png