Ejemplo n.º 1
0
 * Proxy pattern
 *
 * author:	0xnz
 */
interface Subject
{
    public function request();
}
class RealSubject implements Subject
{
    public function request()
    {
        printf("Handling request in real subject\n");
    }
}
class Proxy implements Subject
{
    var $subject;
    public function Proxy($subject)
    {
        $this->subject = $subject;
    }
    // pass request to real subject
    public function request()
    {
        $this->subject->request();
    }
}
$proxy = new Proxy(new RealSubject());
$proxy->request();
Ejemplo n.º 2
0
}
class ProtectionProxy implements ISubject, IProtected
{
    private $subject;
    private $password = "******";
    public function authenticate(string $supplied) : string
    {
        if ($supplied != $this->password) {
            return "Protection Proxy: No Access!";
        }
        $this->subject = new Subject();
        return "Protection Proxy: Authenticated";
    }
    public function request() : string
    {
        if ($this->subject == null) {
            return 'Protection Proxy: Authenticate first!';
        }
        return 'Protection Proxy: Call to ' . $this->subject->request();
    }
}
$subject = new Proxy();
echo $subject->request(), "\n";
echo $subject->request(), "\n";
$subject = new ProtectionProxy();
echo $subject->request(), "\n";
if ($subject instanceof IProtected) {
    echo $subject->authenticate("Secret"), "\n";
    echo $subject->authenticate("Abracadabra"), "\n";
}
echo $subject->request(), "\n";