Design patterns in PHP - PHP TEAM
-
Upload
nishant-shrivastava -
Category
Technology
-
view
237 -
download
0
description
Transcript of Design patterns in PHP - PHP TEAM
<?phpDesign Patterns
In PHP
Nishant Shrivastava@n1shant
• It is a solution to a problem.
•Got a problem, chances are another DEV already has
solved it.
What is Design Pattern?
Why...?
● Improve Readability● Improve Maintainability● Gain better understanding of code
Understanding Patterns..
Let me rephrase it!
Understanding Problem Patterns...
Understanding Patterns...● Problem Patterns● Architectural patterns● Smell in Code
Solutions...
Refactoring
CONTEXT
PROBLEM
SOLUTION
D.R.YDo not repeat Yourself!
K.I.S.SKeep.It.Simple.&.Stupid
Y.A.G.N.IYou ain't gonna need it!
Patterns...● Factory● Singleton● Delegate● Decorator● Strategy● Observer
● Adaptor● State● Iterator● Front Controller● MVC● Active Record
Factory Pattern● Creates Objects without having to instantiate the classes
directly.● Factories creates Objects.● When...
● Keep DRY when complex object creation needs to be re-usable.
● Encapsulate several Object creation in one.
Example : Factory
Class Factory { public function create() { return new Thing(); }}Class Thing { public function doSomething() { echo “Hello PHP'ers!”; }}
$factory = new Factory();$thingObj = $factory->create();$thing->doSomething();
Singleton Pattern● Creates Objects without direct instantiation.● Does not allow more than one instance of self● Ensure only one instance of an obejct at any given time.● When...
● Require only one instance of an class.● Need only one connection to a server.
Example : Singleton
Class SomeSingleton { public static $instance; private function __construct(){} private function __clone(){} public static function getInstance(){ If(!self::$instance instanceof self) {
self::$instance = new self();}Return self::$instance;
}}----------------------------------------------------------------------------------------$someStaticInstance = SomeSingleton::getInstance();
$someInstance = new SomeSingleton();
Decorator Pattern● Decorator adds functionality to an object without changing
the Object's behavior.● When...
● Add features or methods to an object, that are notpart of the Core logic.
● Need extended functionality for specific use cases.
Example : Decorator
class User{ Public $firstName = ''; Public $lastName = '';}
Class UserDecorator{Private $user;Public function __construct(User $user){
$this->user = $user;}Public function getFullName() {
Return “{$this->user->firstName} {$this->user->lastName}”;}
}
Example : Decorator...
$user = new User();
$user->firstName = 'Chuck'; $user->firstName = 'Norris';
$decoratedUser = new UserDecorator($user);echo $decoratedUser->getFullName();
// 'Chuck Norris'
Observer Pattern● Objects(Subject) register other objects (Observer) that
react to state changes of their subject.● In simple words, Observer looks for changes and
do somethingl When...
● State Changes of an Object affect other objects or datasets● Event Handling● Data persistence● Logging
Example : Observer
?>
Thank You!
Nishant Shrivastava@n1shant