beyond design patterns phpnw14
DESCRIPTION
Many people teach design patterns as a fundamental step to Object Oriented Programming. They are so universally seen as important that almost every single programming conference that I have been to has had at least one talk about them. They are quite often used as interview questions to test a candidate's OOP knowledge. However, just like inheritance, they are not needed for OOP. And just like inheritance, they are a distraction rather than a foundation. Instead of focusing on patterns, I suggest focusing on learning about abstraction and communication. Why? Come and find out! Talk at #PHPNW14TRANSCRIPT
Beyond Design PatternsA Guide To Better OOP
Why Learning Object-Oriented
Design From Design Patterns Is Bad
OR:
Object Oriented Design
… is the process of planning a system of interacting objects for the purpose of solving a software problem.
-Wikipedia
What Are Design Patterns?
What Are Design Patterns?
● Commonly Occurring Solutions?
What Are Design Patterns?
● Commonly Occurring Solutions?● Commonly Occurring Structures?
What Are Design Patterns?
● Commonly Occurring Solutions?● Commonly Occurring Structures?● Commonly Occurring Limitations?
What Are Design Patterns?
● Commonly Occurring Solutions?● Commonly Occurring Structures?● Commonly Occurring Limitations?
● All Of The Above?
What Are Design Patterns?
All Of The Above!
What Kind Of Design Patterns Are There?
Creational
Abstract FactoryBuilderFactory MethodObject PoolPrototype
Gang-Of-Four Design Patterns
Creational Structural
Abstract FactoryBuilderFactory MethodObject PoolPrototype
AdapterBridgeCompositeDecoratorFacadeFlyweightProxy
Gang-Of-Four Design Patterns
Creational Structural Behavioral
Abstract FactoryBuilderFactory MethodObject PoolPrototype
AdapterBridgeCompositeDecoratorFacadeFlyweightProxy
Chain Of ResponsibilityCommandInterpreterIteratorMediatorMementoNull ObjectObserverStrategyTemplate Method
Gang-Of-Four Design Patterns
Let’s Try A Different Organization
Shim
FlyweightIteratorNull ObjectObject PoolPrototype
Gang-Of-Four Design PatternsRe-Grouped
Shim Compositional
FlyweightIteratorNull ObjectObject PoolPrototype
AdapterBuilderDecoratorFacadeInterpreterMediatorObserverProxy
Gang-Of-Four Design PatternsRe-Grouped
Shim Compositional Decompositional
FlyweightIteratorNull ObjectObject PoolPrototype
AdapterBuilderDecoratorFacadeInterpreterMediatorObserverProxy
Abstract FactoryBridgeChain Of ResponsibilityCommandFactory MethodMediatorMementoObserverProxyStrategyTemplate Method
Gang-Of-Four Design PatternsRe-Grouped
Compare Groupings
Creational Structural Behavioral
Shim Abstract FactoryObject PoolPrototype
Flyweight IteratorNull Object
Compositional Builder AdapterCompositeDecoratorFacadeProxy
InterpreterMediatorObserver
Decompositional Factory Method BridgeCompositeProxy
Chain Of ResponsibilityCommandMediatorMementoObserverStrategyTemplate Method
Let’s Examine 5 Patterns More Closely
Compare Groupings
Creational Structural Behavioral
Shim Abstract FactoryObject PoolPrototype
Flyweight IteratorNull Object
Compositional Builder AdapterComposite
DecoratorFacadeProxy
InterpreterMediatorObserver
Decompositional Factory Method BridgeComposite
Proxy
Chain Of ResponsibilityCommandMediatorMementoObserverStrategyTemplate Method
Adapter
use Psr\Log\LoggerInterface as Logclass PSR3Logger implements Log {
public function log($level, $msg, array $ctx = array()) {
$severity = $this->convertLevelToSeverity($level);
watchdog(“unknown”, $msg, $ctx, $severity);
}
/* ... */
}
Facade
class EntityMetadataWrapper { public function __construct($type, $data = null, $info = array()) {
$this->type = $type;
$this->info = $info + array(
“langcode” => null,
);
$this->info[“type”] = $type;
if (isset($data)) {
$this->set($data);
}
}
/* ... */
}
Adapter vs Facade
Class 1
Class 2
Class 3
Class n
Class 1
Class m
ExistingCode
“Pattern”Code Class 1
Class 2
Class 3
Class n
OtherCode
API
Adapter vs Facade
Class 1
Class 2
Class 3
Class n
Class 1
Class m
ExistingCode
“Pattern”Code Class 1
Class 2
Class 3
Class n
OtherCode
API
API Exists? Adapter
Adapter vs Facade
Class 1
Class 2
Class 3
Class n
Class 1
Class m
ExistingCode
“Pattern”Code Class 1
Class 2
Class 3
Class n
OtherCode
API
API Exists? AdapterNew API? Facade
They Look The Same!!!???
Adapter Facade
Bridge Decorator Proxy
They All Look The Same!!!???
Adapter Facade
Bridge Decorator Proxy
Adapter Facade
Bridge Decorator Proxy
The Code Is The SameThe Why Is Different
DeDuplicated Groupings
Creational Structural Behavioral
Compositional AdapterComposite
MediatorObserver
Decompositional AdapterComposite
CommandMediatorMementoObserver
DeDuplicated Re-Groupings
Multiple Systems?
Single System?
Single Objects?
Information Flow?
Mediator Command Observer
Structure Adapter Composite Memento
DeDeDuplicated Re-Groupings
Pattern
Information Flow?
Mediator
Structure Adapter
DeDeDuplicated ReRe-Groupings
Pattern
Information Flow?
Mediator
All Design Patterns Do The Same Thing:
All Design Patterns Do The Same Thing:
Control Information Flow
So Let’s Talk About Information Flow
So Let’s Talk About Communication
$msg
A message
$obj
Logic
Message Vs Logic
State
Message Vs Logic
Messages
State
Message Vs Logic
Messages
State
Logic
Message Vs Logic
Messages
State
Logic HERE BE DRAGONS
Why Break Them Apart?
Logic Hybrid Message
Purpose Behavior General Code State
State Stateless Stateful Stateful
Paradigm Functional OOP? Data
$msg = $obj->something();
Ask
Logic Hybrid Message
No Yes Yes
$obj->something($msg);
Tell
Logic Hybrid Message
No Yes Yes
$msg2 = $obj->do($msg);
Translate
Logic Hybrid Message
Yes Yes No
Everything Else Is A Combination Of These
Atoms
Note:
Ask Is Always Stateful
Note:
Ask Is Always StatefulTell Is Always Stateful
Note:
Ask Is Always StatefulTell Is Always Stateful
Translate Can Be Stateless
Note:
Why Does This Matter?
It Lets Us Analyze Object Roles
Object Role Patterns
State? Logic? Mode
Object Role Patterns
State? Logic? Mode
Representer User No Ask/Tell
Representer
class User {
public function getName();
public function isAdmin();
public function setName($name);
}
Object Role Patterns
State? Logic? Mode
Representer User No Ask/Tell
Doer No Yes Tell
Doer
class EmailSystem {
/** * @return boolean */ public function send(Message $msg);
}
Object Role Patterns
State? Logic? Mode
Representer User No Ask/Tell
Doer No Yes Tell
Dispatcher System No Translate
Dispatcher
class Controller {
/** * @return Response */ public function handle(Request $req);
}
Object Role Patterns
State? Logic? Mode
Representer User No Ask/Tell
Doer No Yes Tell
Dispatcher System No Translate
Translator No Yes Translate
Translator
class UserView {
/** * @return string HTML */ public function render(User $user);
}
Object Role Patterns
State? Logic? Mode
Representer User No Ask/Tell
Doer No Yes Tell
Dispatcher System No Translate
Translator No Yes Translate
Maker System Yes Ask
Makers
class UserFactory {
/** * @return User a user object */ public function getUser();
}
Object Role Patterns
State? Logic? Mode
Representer User No Ask/Tell
Doer No Yes Tell
Dispatcher System No Translate
Translator No Yes Translate
Maker System Yes Ask
Object Role Patterns
Representer
Doer
Dispatcher
Translator
Maker
Object Oriented Design
… is the process of planning a system of interacting objects for the purpose of solving a software problem.
-Wikipedia
Object Oriented Design
… is the process of planning a system of interacting objects for the purpose of solving a software problem.
-Wikipedia
Design PatternsAre Important!
And Should Be Used!
But OOP Is Not About “Finding The Correct
Pattern”
Focus on:Communication
InteractionAnd Abstraction
Remember:OOP Is About
Solving Problems
Anthony Ferrarajoind.in/11796
github.com/ircmaxellyoutube.com/ircmaxell