Skip to content

Instantly share code, notes, and snippets.

@eiel
Forked from TakahashiIkki/Abstract_Factory.php
Created December 19, 2019 15:56
Show Gist options
  • Save eiel/10920bf2e14d9bdcab08309be762b6b2 to your computer and use it in GitHub Desktop.
Save eiel/10920bf2e14d9bdcab08309be762b6b2 to your computer and use it in GitHub Desktop.
<?php
namespace RadioControllerCar;
/* ラジコンの抽象クラス */
interface CarInterface
{
public function send($message);
}
/* リモコンの抽象クラス */
interface ControllerInterface
{
/**
* @param $car CarInterface
*/
public function connect($car);
public function send_forward();
public function send_back();
}
class TakaraTommyController implements ControllerInterface
{
/** @var CarInterface */
private $car;
public function connect($car)
{
$this->car = $car;
}
public function send_forward()
{
$this->car->send("gogo");
}
public function send_back()
{
$this->car->send("backback");
}
}
class HoriController implements ControllerInterface
{
/** @var CarInterface */
private $car;
private $protocol;
public function __construct($protocol)
{
$this->protocol = $protocol;
}
public function connect($car)
{
$this->car = $car;
}
public function send_forward()
{
if ($this->protocol === "TakaraTommy") {
$this->car->send("gogo");
} else {
$this->car->send("forward");
}
}
public function send_back()
{
if ($this->protocol === "TakaraTommy") {
$this->car->send("backback");
} else {
$this->car->send("back");
}
}
}
class TakaraTommyCar implements CarInterface
{
// メッセージを受け取ったときの処理
public function send($message)
{
if ($message === "gogo") {
$this->forward();
}
if ($message === "backback") {
$this->back();
}
}
private function forward()
{
echo "前進! \r\n";
}
private function back()
{
echo "後退! \r\n";
}
}
class GenericCar implements CarInterface
{
// メッセージをうけとったときの処理
public function send($message)
{
if ($message === "forward") {
echo "前進!!!! \r\n";
}
if ($message === "back") {
echo "後退!!!! \r\n";
}
}
}
interface RadioControllerFactoryInterface
{
/**
* @return ControllerInterface
*/
public function create_controller();
/**
* @param CarInterface $channel
* @return CarInterface
*/
public function create_car();
}
// ConcreteFactory
class TakaraTommyRadioControllerFactory implements RadioControllerFactoryInterface
{
/**
* @return ControllerInterface
*/
public function create_controller()
{
return new TakaraTommyController();
}
/**
* @return CarInterface|TakaraTommyCar
*/
public function create_car()
{
return new TakaraTommyCar();
}
}
// ConcreteFactory
class TakaraTommyWithHoriRadioControllerFactory implements RadioControllerFactoryInterface
{
/**
* @return ControllerInterface
*/
public function create_controller()
{
return new HoriController("TakaraTommy");
}
/**
* @return CarInterface|TakaraTommyCar
*/
public function create_car()
{
return new TakaraTommyCar();
}
}
// ConcreteFactory
class GenericRadioControllerFactory implements RadioControllerFactoryInterface
{
/**
* @return ControllerInterface
*/
public function create_controller()
{
return new HoriController("Generic");
}
/**
* @return CarInterface|TakaraTommyCar
*/
public function create_car()
{
return new GenericCar();
}
}
class CarCreator
{
/**
* @param RadioControllerFactoryInterface $factory
* @return ControllerInterface
*/
public function build($factory)
{
$car = $factory->create_car();
$controller = $factory->create_controller();
$controller->connect($car);
return $controller;
}
}
class User1
{
/**
* @param ControllerInterface $controller
*/
public function play($controller)
{
echo "play user1 \r\n";
/* 前進 */
$controller->send_forward();
/* 後退 */
$controller->send_back();
}
}
class User2
{
/**
* @param ControllerInterface $controller
*/
public function play($controller)
{
echo "play user2 \r\n";
/* 前進 */
$controller->send_forward();
/* 後退 */
$controller->send_forward();
}
}
$carCreator = new CarCreator();
$user1 = new User1();
$user2 = new User2();
$takaraTommyfactory = new TakaraTommyRadioControllerFactory();
$takaraTommyWithHoriFactory = new TakaraTommyWithHoriRadioControllerFactory();
$genericFactory = new GenericRadioControllerFactory();
$controller = $carCreator->build($takaraTommyfactory);
$user1->play($controller);
$user2->play($controller);
$controller = $carCreator->build($takaraTommyWithHoriFactory);
$user1->play($controller);
$user2->play($controller);
$controller = $carCreator->build($genericFactory);
$user1->play($controller);
$user2->play($controller);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment