解耦一个对象的实现与抽象,这样两者可以独立地变化。
Formatter.php
<?php
declare(strict_types=1);
namespace DesignPatternsStructuralBridge;
interface Formatter
{
public function format(string $text): string;
}
PlainTextFormatter.php
<?php
declare(strict_types=1);
namespace DesignPatternsStructuralBridge;
class PlainTextFormatter implements Formatter
{
public function format(string $text): string
{
return $text;
}
}
HtmlFormatter.php
<?php
declare(strict_types=1);
namespace DesignPatternsStructuralBridge;
class HtmlFormatter implements Formatter
{
public function format(string $text): string
{
return sprintf("<p>%s</p>", $text);
}
}
Service.php
<?php
declare(strict_types=1);
namespace DesignPatternsStructuralBridge;
abstract class Service
{
public function __construct(protected Formatter $implementation)
{
}
public function setImplementation(Formatter $printer)
{
$this->implementation = $printer;
}
abstract public function get(): string;
}
HelloWorldService.php
<?php
declare(strict_types=1);
namespace DesignPatternsStructuralBridge;
class HelloWorldService extends Service
{
public function get(): string
{
return $this->implementation->format("Hello World");
}
}
PingService.php
<?php
declare(strict_types=1);
namespace DesignPatternsStructuralBridge;
class PingService extends Service
{
public function get(): string
{
return $this->implementation->format("pong");
}
}
Tests/BridgeTest.php
<?php
declare(strict_types=1);
namespace DesignPatternsStructuralBridgeTests;
use DesignPatternsStructuralBridgeHelloWorldService;
use DesignPatternsStructuralBridgeHtmlFormatter;
use DesignPatternsStructuralBridgePlainTextFormatter;
use PHPUnitFrameworkTestCase;
class BridgeTest extends TestCase
{
public function testCanPrintUsingThePlainTextFormatter()
{
$service = new HelloWorldService(new PlainTextFormatter());
$this->assertSame("Hello World", $service->get());
}
public function testCanPrintUsingTheHtmlFormatter()
{
$service = new HelloWorldService(new HtmlFormatter());
$this->assertSame("<p>Hello World</p>", $service->get());
}
}
OceanBase 开发者中心(OceanBase Developer Center,ODC)提供模拟数据功能供您在测试数据库性能或者验证功能等需要大量模拟数...
SQL Trace 能够交互式的提供上一次执行的 SQL 请求执行过程信息及各阶段的耗时。SQL Trace 开关SQL Trace 功能默认是关闭的,可...
分布式计划根据以下步骤分析查询问题:通过查看(g)v$plan_cache_plan_stat视图、(g)v$sql_audit中对执行计划类型的记录,确定是...
在 OceanBase 开发者中心(OceanBase Developer Center,ODC)单击连接名进入连接后,在左导航栏中单击 程序包 标签可以查看程序...
SQL 语句在普通 SQL 语句中,OceanBase 数据库支持如下 3 种注释方法:从#到行尾从--到行尾从数据库对象在 DDL 语句...