kamranahmedse / design-patterns-for-humans

An ultra-simplified explanation to design patterns

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proxy

gokutek opened this issue · comments

commented

I have a question on the proxy demo code.
Is it necessary to let Security inherit from Door?
From the GoF book, I'm think it should be.

Hey, there could be multiple ways to implement the proxy; you can do it the way I explained in the example or you could do it the other way using inheritance e.g. you could have a default Door class and then you could have a SecuredDoor class extending from the Door class while overriding the methods of the class and adding some functionality on top of each method i.e.

class Door {
    public function open() {
        echo 'Opening the door';
    }

    public function close() {
        echo 'Closing the door';
    }
}

then the proxy class

class SecuredDoor extends Door {
    public function open () {
        echo 'Securing the door';
        parent::open();
    }
}

then use it

$door = new SecuredDoor();

$door->open();
// We don't want to add any additional functionality 
// so it will be proxied to the parent class
$door->close();

Hope it helps.

commented

aha, I got it, thanks for your reply. And, thanks for your share. 👍