![d35848a68a654997ccc5e3b1c9022f9b.png](https://img-blog.csdnimg.cn/img_convert/d35848a68a654997ccc5e3b1c9022f9b.png)
目的[1]
解耦一个对象使抽象与实现分离,这样两者可以独立地变化。
例子
https://github.com/symfony/doctrine-bridgegithub.comUML图
![7f2f17c4db08fd11787d4f400966ed25.png](https://img-blog.csdnimg.cn/img_convert/7f2f17c4db08fd11787d4f400966ed25.png)
★官方PHP高级学习交流社群「点击」管理整理了一些资料,BAT等一线大厂进阶知识体系备好(相关学习资料以及笔面试题)以及不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货
代码
- Formatter.php
<?php
namespace DesignPatternsStructuralBridge;
/**
* 创建格式化接口。
*/
interface FormatterInterface
{
public function format(string $text);
}
- PlainTextFormatter.php
<?php
namespace DesignPatternsStructuralBridge;
/**
* 创建 PlainTextFormatter 文本格式类实现 FormatterInterface 接口。
*/
class PlainTextFormatter implements FormatterInterface
{
/**
* 返回字符串格式。
*/
public function format(string $text)
{
return $text;
}
}
- HtmlFormatter.php
<?php
namespace DesignPatternsStructuralBridge;
/**
* 创建 HtmlFormatter HTML 格式类实现 FormatterInterface 接口。
*/
class HtmlFormatter implements FormatterInterface
{
/**
* 返回 HTML 格式。
*/
public function format(string $text)
{
return sprintf('<p>%s</p>', $text);
}
}
- Service.php
<?php
namespace DesignPatternsStructuralBridge;
/**
* 创建抽象类 Service。
*/
abstract class Service
{
/**
* @var FormatterInterface
* 定义实现属性。
*/
protected $implementation;
/**
* @param FormatterInterface $printer
* 传入 FormatterInterface 实现类对象。
*/
public function __construct(FormatterInterface $printer)
{
$this->implementation = $printer;
}
/**
* @param FormatterInterface $printer
* 和构造方法的作用相同。
*/
public function setImplementation(FormatterInterface $printer)
{
$this->implementation = $printer;
}
/**
* 创建抽象方法 get() 。
*/
abstract public function get();
}
- HelloWorldService.php
<?php
namespace DesignPatternsStructuralBridge;
/**
* 创建 Service 子类 HelloWorldService 。
*/
class HelloWorldService extends Service
{
/**
* 定义抽象方法 get() 。
* 根据传入的格式类定义来格式化输出 'Hello World' 。
*/
public function get()
{
return $this->implementation->format('Hello World');
}
}
测试
- Tests/BridgeTest.php
<?php
namespace DesignPatternsStructuralBridgeTests;
use DesignPatternsStructuralBridgeHelloWorldService;
use DesignPatternsStructuralBridgeHtmlFormatter;
use DesignPatternsStructuralBridgePlainTextFormatter;
use PHPUnitFrameworkTestCase;
/**
* 创建自动化测试单元 BridgeTest 。
*/
class BridgeTest extends TestCase
{
/**
* 使用 HelloWorldService 分别测试文本格式实现类和 HTML 格式实
* 现类。
*/
public function testCanPrintUsingThePlainTextPrinter()
{
$service = new HelloWorldService(new PlainTextFormatter());
$this->assertEquals('Hello World', $service->get());
// 现在更改实现方法为使用 HTML 格式器。
$service->setImplementation(new HtmlFormatter());
$this->assertEquals('<p>Hello World</p>', $service->get());
}
}
![1cddab0b92decba94a94a8c920920831.png](https://img-blog.csdnimg.cn/img_convert/1cddab0b92decba94a94a8c920920831.png)
![d78b3b5d2ddcf7cc56e521e736f060ee.png](https://img-blog.csdnimg.cn/img_convert/d78b3b5d2ddcf7cc56e521e736f060ee.png)
![fbf6a7390feda7a4c56400177ccefdee.png](https://img-blog.csdnimg.cn/img_convert/fbf6a7390feda7a4c56400177ccefdee.png)
★如果喜欢我的文章,想与更多资深开发者一起交流学习的话,获取更多大厂面试相关技术咨询和指导,欢迎加入我们的群啊,暗号:phpzh(群号码856460874)。
2020年最新PHP进阶教程,全系列!
![c301fbeb2fcc8807d2159a10fe796343.gif](https://img-blog.csdnimg.cn/img_convert/c301fbeb2fcc8807d2159a10fe796343.gif)
参考
- ^内容不错的话希望大家支持鼓励下点个赞/喜欢,欢迎一起来交流;另外如果有什么问题和想看的内容可以在评论提出