Design patterns in PHP - PHP TEAM

22
<?php Design Patterns In PHP Nishant Shrivastava @n1shant

description

Covers some of the Design patterns and some common ways to re-factor code. Also, how to make things in much better way, which writing your code.

Transcript of Design patterns in PHP - PHP TEAM

Page 1: Design patterns in PHP - PHP TEAM

<?phpDesign Patterns

In PHP

Nishant Shrivastava@n1shant

Page 2: Design patterns in PHP - PHP TEAM

• It is a solution to a problem.

•Got a problem, chances are another DEV already has

solved it.

What is Design Pattern?

Page 3: Design patterns in PHP - PHP TEAM

Why...?

● Improve Readability● Improve Maintainability● Gain better understanding of code

Page 4: Design patterns in PHP - PHP TEAM

Understanding Patterns..

Page 5: Design patterns in PHP - PHP TEAM

Let me rephrase it!

Understanding Problem Patterns...

Page 6: Design patterns in PHP - PHP TEAM

Understanding Patterns...● Problem Patterns● Architectural patterns● Smell in Code

Page 7: Design patterns in PHP - PHP TEAM

Solutions...

Refactoring

CONTEXT

PROBLEM

SOLUTION

Page 8: Design patterns in PHP - PHP TEAM

D.R.YDo not repeat Yourself!

Page 9: Design patterns in PHP - PHP TEAM

K.I.S.SKeep.It.Simple.&.Stupid

Page 10: Design patterns in PHP - PHP TEAM

Y.A.G.N.IYou ain't gonna need it!

Page 11: Design patterns in PHP - PHP TEAM

Patterns...● Factory● Singleton● Delegate● Decorator● Strategy● Observer

● Adaptor● State● Iterator● Front Controller● MVC● Active Record

Page 12: Design patterns in PHP - PHP TEAM

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.

Page 13: Design patterns in PHP - PHP TEAM

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();

Page 14: Design patterns in PHP - PHP TEAM

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.

Page 15: Design patterns in PHP - PHP TEAM

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();

Page 16: Design patterns in PHP - PHP TEAM

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.

Page 17: Design patterns in PHP - PHP TEAM

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}”;}

}

Page 18: Design patterns in PHP - PHP TEAM

Example : Decorator...

$user = new User();

$user->firstName = 'Chuck'; $user->firstName = 'Norris';

$decoratedUser = new UserDecorator($user);echo $decoratedUser->getFullName();

// 'Chuck Norris'

Page 19: Design patterns in PHP - PHP TEAM

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

Page 20: Design patterns in PHP - PHP TEAM

Example : Observer

Page 21: Design patterns in PHP - PHP TEAM

?>

Page 22: Design patterns in PHP - PHP TEAM

Thank You!

Nishant Shrivastava@n1shant