<?php
declare(strict_types=1);
// php 技术群:781742505
/**
* Interface Door
*/
interface Door
{
/**
* @return mixed
*/
public function open();
/**
* @return mixed
*/
public function close();
}
/**
* Class LabDoor
*/
class LabDoor implements Door
{
/**
* @return mixed|void
*/
public function open()
{
echo "Opening lab door";
}
/**
* @return mixed|void
*/
public function close()
{
echo "Closing the lab door";
}
}
/**
* Class SecuredDoor
*/
class SecuredDoor
{
/**
* @var Door
*/
protected $door;
/**
* SecuredDoor constructor.
*
* @param Door $door
*/
public function __construct(Door $door)
{
$this->door = $door;
}
/**
* @param $password
*/
public function open($password)
{
if ($this->authenticate($password)) {
$this->door->open();
} else {
echo "Big no! It ain't possible.";
}
}
/**
* @param $password
*
* @return bool
*/
public function authenticate($password)
{
return $password === '$ecr@t';
}
/**
*
*/
public function close()
{
$this->door->close();
}
}
$door = new SecuredDoor(new LabDoor());
$door->open('invalid'); // Big no! It ain't possible.
$door->open('$ecr@t'); // Opening lab door
$door->close(); // Closing lab door
代理模式
最新推荐文章于 2024-09-28 22:54:00 发布