Skip to content

Instantly share code, notes, and snippets.

@TakahashiIkki
Last active December 19, 2019 15:56
Show Gist options
  • Save TakahashiIkki/ad44d2128ffcca40a76b98b695456141 to your computer and use it in GitHub Desktop.
Save TakahashiIkki/ad44d2128ffcca40a76b98b695456141 to your computer and use it in GitHub Desktop.
<?php
/* ラジコンの抽象クラス */
interface RadioControlCarInterface
{
public function forward();
public function back();
}
/* リモコンの抽象クラス */
interface RemoteControllerInterface
{
/**
* RemoteControllerInterface constructor.
* @param RemoteCommunicateTipFactoryInterface $remote_communicate_tip_factory
*/
public function __construct($remote_communicate_tip_factory);
public function send_forward();
public function send_back();
}
class TakaraTommyRemoteController implements RemoteControllerInterface
{
/** @var TakaraTommyRadioControlCar */
private $radio_controller;
/**
* TakaraTommyRemoteController constructor.
* @param TakaraTommyRemoteCommunicateTipFactory $remote_communicate_tip_factory
*/
public function __construct($remote_communicate_tip_factory)
{
$this->radio_controller = $remote_communicate_tip_factory->create_relate_radio_controller();
}
public function send_forward()
{
$this->radio_controller->forward();
}
public function send_back()
{
$this->radio_controller->back();
}
}
class TakaraTommyRadioControlCar implements RadioControlCarInterface
{
public function forward()
{
echo "前進! \r\n";
}
public function back()
{
echo "後退! \r\n";
}
}
interface RadioControllerFactoryInterface
{
/**
* @param RadioControlCarInterface $radio_controller
* @return RemoteControllerInterface
*/
public function create_remote_controller();
}
class TakaraTommyRadioControllerFactory implements RadioControllerFactoryInterface
{
/**
* @param RadioControlCarInterface $radio_controller
* @return RemoteControllerInterface
*/
public function create_remote_controller()
{
return new TakaraTommyRemoteController(new TakaraTommyRemoteCommunicateTipFactory());
}
}
/**
* リモコンとラジコンとの通信用のチップ
* Interface RemoteCommunicateTipFactoryInterface
*/
interface RemoteCommunicateTipFactoryInterface
{
/**
* @return RadioControlCarInterface
*/
public function create_relate_radio_controller();
}
class TakaraTommyRemoteCommunicateTipFactory implements RemoteCommunicateTipFactoryInterface
{
public function create_relate_radio_controller()
{
return new TakaraTommyRadioControlCar();
}
}
class User
{
/** @var RemoteControllerInterface */
private $remote_controller;
/**
* User constructor.
* @param RadioControllerFactoryInterface $radicon_facotry
*/
public function __construct($radicon_facotry)
{
$this->remote_controller = $radicon_facotry->create_remote_controller();
}
public function play()
{
/* 前進 */
$this->remote_controller->send_forward();
/* 後退 */
$this->remote_controller->send_back();
}
}
$takara_tommy_radicon_facotry = new TakaraTommyRadioControllerFactory();
$user = new User($takara_tommy_radicon_facotry);
$user->play();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment