php与设计模式-桥模式

桥模式

用于将抽象和实现解耦,使得两者可以独立地变化。

模拟毛笔:
现需要提供大中小3种型号的画笔,能够绘制5种不同颜色,如果使用蜡笔,我们需要准备3*5=15支蜡笔,也就是说必须准备15个具体的蜡笔类。而如果使用毛笔的话,只需要3种型号的毛笔,外加5个颜料盒,用3+5=8个类就可以实现15支蜡笔的功能。
实际上,蜡笔和毛笔的关键一个区别就在于笔和颜色是否能够分离。即将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化"。关键就在于能否脱耦。蜡笔的颜色和蜡笔本身是分不开的,所以就造成必须使用15支色彩、大小各异的蜡笔来绘制图画。而毛笔与颜料能够很好的脱耦,各自独立变化,便简化了操作。在这里,抽象层面的概念是:“毛笔用颜料作画”,而在实现时,毛笔有大中小三号,颜料有红绿蓝黑白等5种,于是便可出现3×5种组合。每个参与者(毛笔与颜料)都可以在自己的自由度上随意转换。

蜡笔由于无法将笔与颜色分离,造成笔与颜色两个自由度无法单独变化,使得只有创建15种对象才能完成任务。
Demo

/******************************Abstraction **************************/ 
/**
 * 
 * Abstraction抽象类的接口
 * @author guisu
 *
 */ 
abstract class BrushPenAbstraction { 
    protected $_implementorColor = null; 
 
    /**
     * 
     * Enter description here ...
     * @param Color $color
     */ 
    public function setImplementorColor(ImplementorColor $color) { 
        $this->_implementorColor = $color; 
    } 
    /**
     * 
     * Enter description here ...
     */ 
    public abstract function operationDraw(); 
} 
/******************************RefinedAbstraction **************************/ 
/**
 * 
 * 扩充由Abstraction;大毛笔
 * @author guisu
 *
 */ 
class BigBrushPenRefinedAbstraction extends BrushPenAbstraction { 
    public function operationDraw() { 
        echo 'Big and ', $this->_implementorColor->bepaint (), ' drawing'; 
    } 
} 
/**
 * 
 * 扩充由Abstraction;中毛笔
 * @author guisu
 *
 */ 
class MiddleBrushPenRefinedAbstraction extends BrushPenAbstraction { 
    public function operationDraw() { 
        echo 'Middle and ', $this->_implementorColor->bepaint (), ' drawing'; 
    } 
} 
/**
 * 
 * 扩充由Abstraction;小毛笔
 * @author guisu
 *
 */ 
class SmallBrushPenRefinedAbstraction extends BrushPenAbstraction { 
    public function operationDraw() { 
        echo 'Small and ', $this->_implementorColor->bepaint(), ' drawing'; 
    } 
} 
 
/******************************Implementor **************************/ 
/**
 * 实现类接口(Implementor)
 * 
 * @author mo-87
 *
 */ 
class ImplementorColor { 
    protected $value; 
 
    /**
     * 着色
     * 
     */ 
    public  function bepaint(){ 
        echo $this->value; 
    } 
} 
/******************************oncrete Implementor **************************/ 
class oncreteImplementorRed extends ImplementorColor { 
    public function __construct() { 
        $this->value = "red"; 
    } 
    /**
     * 可以覆盖
     */ 
    public function bepaint() { 
        echo $this->value; 
    } 
} 
 
class oncreteImplementorBlue extends ImplementorColor { 
    public function __construct() { 
        $this->value = "blue"; 
    } 
} 
 
class oncreteImplementorGreen extends ImplementorColor { 
    public function __construct() { 
        $this->value = "green"; 
    } 
} 
 
class oncreteImplementorWhite extends ImplementorColor { 
    public function __construct() { 
        $this->value = "white"; 
    } 
} 
 
class oncreteImplementorBlack extends ImplementorColor { 
    public function __construct() { 
        $this->value = "black"; 
    } 
} 
/**
 * 
 * 客户端程序
 * @author guisu
 *
 */ 
class Client { 
    public static function Main() { 
 
        //小笔画红色 
        $objRAbstraction = new SmallBrushPenRefinedAbstraction(); 
        $objRAbstraction->setImplementorColor(new oncreteImplementorRed()); 
        $objRAbstraction->operationDraw(); 
    } 
} 
Client::Main(); 

请关注我的订阅号

订阅号.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码哥说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值