php 示例
最后更新于:2022-04-02 04:19:45
[TOC]
## 示例
输出 ``` Abstraction: Base operation with: ConcreteImplementationA: Here's the result on the platform A. ExtendedAbstraction: Extended operation with: ConcreteImplementationB: Here's the result on the platform B. ``` ### 渲染层 页面层作为抽象部分, 而渲染层则作为实现部分
输出 ```
';
main.php
``` implementation = $implementation; } public function operation(): string { return "Abstraction: Base operation with:\n" . $this->implementation->operationImplementation(); } } class ExtendedAbstraction extends Abstraction { public function operation(): string { return "ExtendedAbstraction: Extended operation with:\n" . $this->implementation->operationImplementation(); } } class ConcreteImplementationA implements Implementation { public function operationImplementation(): string { return "ConcreteImplementationA: Here's the result on the platform A.\n"; } } class ConcreteImplementationB implements Implementation { public function operationImplementation(): string { return "ConcreteImplementationB: Here's the result on the platform B.\n"; } } function clientCode(Abstraction $abstraction) { // ... echo $abstraction->operation(); // ... } $implementation = new ConcreteImplementationA(); $abstraction = new Abstraction($implementation); clientCode($abstraction); echo "\n"; $implementation = new ConcreteImplementationB(); $abstraction = new ExtendedAbstraction($implementation); clientCode($abstraction); ```输出 ``` Abstraction: Base operation with: ConcreteImplementationA: Here's the result on the platform A. ExtendedAbstraction: Extended operation with: ConcreteImplementationB: Here's the result on the platform B. ``` ### 渲染层 页面层作为抽象部分, 而渲染层则作为实现部分
main.php
``` renderer = $renderer; } public function changeRenderer(Renderer $renderer): void { $this->renderer = $renderer; } abstract public function view(): string; } class ProductPage extends Page { protected $product; public function __construct(Renderer $renderer, Product $product) { parent::__construct($renderer); $this->product = $product; } public function view(): string { return $this->renderer->renderParts([ $this->renderer->renderHeader(), $this->renderer->renderTitle($this->product->getTitle()), $this->renderer->renderTextBlock($this->product->getDescription()), $this->renderer->renderImage($this->product->getImage()), $this->renderer->renderLink("/cart/add/" . $this->product->getId(), "Add to cart"), $this->renderer->renderFooter() ]); } } class Product { private $id, $title, $description, $image, $price; public function __construct( string $id, string $title, string $description, string $image, float $price ) { $this->id = $id; $this->title = $title; $this->description = $description; $this->image = $image; $this->price = $price; } public function getId(): string { return $this->id; } public function getTitle(): string { return $this->title; } public function getDescription(): string { return $this->description; } public function getImage(): string { return $this->image; } public function getPrice(): float { return $this->price; } } class HTMLRenderer implements Renderer { public function renderTitle(string $title): string { return "$title
"; } public function renderTextBlock(string $text): string { return "$text
";
}
public function renderImage(string $url): string
{
return "";
}
public function renderLink(string $url, string $title): string
{
return "$title";
}
public function renderHeader(): string
{
return "";
}
public function renderFooter(): string
{
return "";
}
public function renderParts(array $parts): string
{
return implode("\n", $parts);
}
}
class JsonRenderer implements Renderer
{
public function renderTitle(string $title): string
{
return '"title": "' . $title . '"';
}
public function renderTextBlock(string $text): string
{
return '"text": "' . $text . '"';
}
public function renderImage(string $url): string
{
return '"img": "' . $url . '"';
}
public function renderLink(string $url, string $title): string
{
return '"link": {"href": "' . $title . '", "title": "' . $title . '""}';
}
public function renderHeader(): string
{
return '';
}
public function renderFooter(): string
{
return '';
}
public function renderParts(array $parts): string
{
return "{\n" . implode(",\n", array_filter($parts)) . "\n}";
}
}
function clientCode(Page $page)
{
// ...
echo $page->view();
// ...
}
/**
* The client code can be executed with any pre-configured combination of the
* Abstraction+Implementation.
*/
$HTMLRenderer = new HTMLRenderer();
$JSONRenderer = new JsonRenderer();
$product = new Product("123", "Star Wars, episode1",
"A long time ago in a galaxy far, far away...",
"/images/star-wars.jpeg", 39.95);
$page = new ProductPage($HTMLRenderer, $product);
clientCode($page);
echo "\n\n";
$page->changeRenderer($JSONRenderer);
clientCode($page);
```
输出 ```
Star Wars, episode1
A long time ago in a galaxy far, far away...
Add to cart
{
"title": "Star Wars, episode1",
"text": "A long time ago in a galaxy far, far away...",
"img": "/images/star-wars.jpeg",
"link": {"href": "Add to cart", "title": "Add to cart""}
}
```