🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 示例 <details> <summary>index.php</summary> ``` <?php // 抽象工厂 interface AbstractFactory { public function createProductA(): AbstractProductA; public function createProductB(): AbstractProductB; } class ConcreteFactory1 implements AbstractFactory { public function createProductA(): AbstractProductA { return new ConcreteProductA1(); } public function createProductB(): AbstractProductB { return new ConcreteProductB1(); } } class ConcreteFactory2 implements AbstractFactory { public function createProductA(): AbstractProductA { return new ConcreteProductA2(); } public function createProductB(): AbstractProductB { return new ConcreteProductB2(); } } // 抽象产品A interface AbstractProductA { public function usefulFunctionA(): string; } class ConcreteProductA1 implements AbstractProductA { public function usefulFunctionA(): string { return "The result of the product A1."; } } class ConcreteProductA2 implements AbstractProductA { public function usefulFunctionA(): string { return "The result of the product A2."; } } // 抽象产品B interface AbstractProductB { public function usefulFunctionB(): string; public function anotherUsefulFunctionB(AbstractProductA $collaborator): string; } class ConcreteProductB1 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B1."; } public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return $result; } } class ConcreteProductB2 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B2."; } public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return $result; } } function clientCode(AbstractFactory $factory) { $productA = $factory->createProductA(); $productB = $factory->createProductB(); echo $productB->usefulFunctionB() . "\n"; echo $productB->anotherUsefulFunctionB($productA) . "\n"; } clientCode(new ConcreteFactory1()); echo "\n"; clientCode(new ConcreteFactory2()); ``` </details> <br /> 输出 ``` The result of the product B1. The result of the product A1. The result of the product B2. The result of the product A2. ``` ### 渲染引擎示例 <details> <summary>index.php</summary> ``` <?php // 抽象工厂 interface TemplateFactory { public function createTitleTemplate(): TitleTemplate; public function createPageTemplate(): PageTemplate; public function getRenderer(): TemplateRenderer; } class TwigTemplateFactory implements TemplateFactory { public function createTitleTemplate(): TitleTemplate { return new TwigTitleTemplate(); } public function createPageTemplate(): PageTemplate { return new TwigPageTemplate($this->createTitleTemplate()); } public function getRenderer(): TemplateRenderer { return new TwigRenderer(); } } class PHPTemplateFactory implements TemplateFactory { public function createTitleTemplate(): TitleTemplate { return new PHPTemplateTitleTemplate(); } public function createPageTemplate(): PageTemplate { return new PHPTemplatePageTemplate($this->createTitleTemplate()); } public function getRenderer(): TemplateRenderer { return new PHPTemplateRenderer(); } } // 抽象产品 interface TitleTemplate { public function getTemplateString(): string; } class TwigTitleTemplate implements TitleTemplate { public function getTemplateString(): string { return "<h1>{{ title }}</h1>"; } } class PHPTemplateTitleTemplate implements TitleTemplate { public function getTemplateString(): string { return "<h1><?= \$title; ?></h1>"; } } // 抽象产品 interface PageTemplate { public function getTemplateString(): string; } abstract class BasePageTemplate implements PageTemplate { protected $titleTemplate; public function __construct(TitleTemplate $titleTemplate) { $this->titleTemplate = $titleTemplate; } } class TwigPageTemplate extends BasePageTemplate { public function getTemplateString(): string { $renderedTitle = $this->titleTemplate->getTemplateString(); return <<<HTML <div class="page"> $renderedTitle <article class="content">{{ content }}</article> </div> HTML; } } class PHPTemplatePageTemplate extends BasePageTemplate { public function getTemplateString(): string { $renderedTitle = $this->titleTemplate->getTemplateString(); return <<<HTML <div class="page"> $renderedTitle <article class="content"><?= \$content; ?></article> </div> HTML; } } interface TemplateRenderer { public function render(string $templateString, array $arguments = []): string; } class TwigRenderer implements TemplateRenderer { public function render(string $templateString, array $arguments = []): string { return \Twig::render($templateString, $arguments); } } class PHPTemplateRenderer implements TemplateRenderer { public function render(string $templateString, array $arguments = []): string { extract($arguments); ob_start(); eval(' ?>' . $templateString . '<?php '); $result = ob_get_contents(); ob_end_clean(); return $result; } } class Page { public $title; public $content; public function __construct($title, $content) { $this->title = $title; $this->content = $content; } public function render(TemplateFactory $factory): string { $pageTemplate = $factory->createPageTemplate(); $renderer = $factory->getRenderer(); return $renderer->render($pageTemplate->getTemplateString(), [ 'title' => $this->title, 'content' => $this->content ]); } } $page = new Page('Sample page', 'This it the body.'); echo $page->render(new PHPTemplateFactory()); ``` </details> <br /> 输出 ``` <div class="page"> <h1>Sample page</h1> <article class="content">This it the body.</article> </div>% ```