Абстрактна фабрика: відмінності між версіями
[перевірена версія] | [неперевірена версія] |
Вилучено вміст Додано вміст
Рядок 229:
</source>
}}
=== PHP ===
{{Hider_hiding
| title = Приклад реалізації на мові [[PHP]]
| content =
<?php
// Загальний Інтерфейс реалізації
interface GuiFactoryInterface
{
public function buildButton(): ButtonInterface;
public function buildCheckBox(): CheckBoxInterface;
}
interface ButtonInterface
{
public function draw();
}
interface CheckBoxInterface
{
public function draw();
}
// Кінцева реалізація
class ButtonBootstrap implements ButtonInterface
{
public function draw() {return __CLASS__;}
}
class CheckBoxBootstrap implements CheckBoxInterface
{
public function draw() {return __CLASS__;}
}
class ButtonSemanticUI implements ButtonInterface
{
public function draw() {return __CLASS__;}
}
class CheckBoxSemanticUI implements CheckBoxInterface
{
public function draw() {return __CLASS__;}
}
// Інтерфейси для звязку однопитності
class BootstrapFactory implements GuiFactoryInterface
{
public function buildButton(): ButtonInterface
{
return new ButtonBootstrap();
}
public function buildCheckBox(): CheckBoxInterface
{
return new CheckBoxBootstrap();
}
}
class SemanticUIFactory implements GuiFactoryInterface
{
public function buildButton(): ButtonInterface
{
return new ButtonSemanticUI();
}
public function buildCheckBox(): CheckBoxInterface
{
return new CheckBoxSemanticUI();
}
}
class GuiKitFactory
{
public function getFactory($type): GuiFactoryInterface
{
switch ($type) {
case 'bootstrap':
$factory = new BootstrapFactory();
break;
case 'semantic_ui':
$factory = new SemanticUIFactory();
break;
default:
throw new Exception('Невідомий тип фабрики [' . $type . ']');
}
return $factory;
}
}
class CreationPatternController
{
private $guiKit;
public function __construct()
{
$this->guiKit = (new GuiKitFactory())->getFactory('bootstrap');
// OR
//$this->guiKit = (new GuiKitFactory())->getFactory('semantic_ui');
}
function AbstractFactory()
{
$result[] = $this->guiKit->buildCheckbox()->draw();
$result[] = $this->guiKit->buildButton()->draw();
return $result;
}
}
$drawer = new CreationPatternController();
$drawer->AbstractFactory();
}}
== Примітки ==
{{reflist}}
|