Beyond Design Patterns 
A Guide To Better OOP
OR: 
Why Learning 
Object-Oriented 
Design From Design 
Patterns Is Bad
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?
Gang-Of-Four Design Patterns 
Creational 
Abstract Factory 
Builder 
Factory Method 
Object Pool 
Prototype
Gang-Of-Four Design Patterns 
Creational Structural 
Abstract Factory 
Builder 
Factory Method 
Object Pool 
Prototype 
Adapter 
Bridge 
Composite 
Decorator 
Facade 
Flyweight 
Proxy
Gang-Of-Four Design Patterns 
Creational Structural Behavioral 
Abstract Factory 
Builder 
Factory Method 
Object Pool 
Prototype 
Adapter 
Bridge 
Composite 
Decorator 
Facade 
Flyweight 
Proxy 
Chain Of Responsibility 
Command 
Interpreter 
Iterator 
Mediator 
Memento 
Null Object 
Observer 
Strategy 
Template Method
Let’s Try A Different 
Organization
Gang-Of-Four Design Patterns 
Shim 
Flyweight 
Iterator 
Null Object 
Object Pool 
Prototype 
Re-Grouped
Gang-Of-Four Design Patterns 
Re-Grouped 
Shim Compositional 
Flyweight 
Iterator 
Null Object 
Object Pool 
Prototype 
Adapter 
Builder 
Decorator 
Facade 
Interpreter 
Mediator 
Observer 
Proxy
Gang-Of-Four Design Patterns 
Re-Grouped 
Shim Compositional Decompositional 
Flyweight 
Iterator 
Null Object 
Object Pool 
Prototype 
Adapter 
Builder 
Decorator 
Facade 
Interpreter 
Mediator 
Observer 
Proxy 
Abstract Factory 
Bridge 
Chain Of Responsibility 
Command 
Factory Method 
Mediator 
Memento 
Observer 
Proxy 
Strategy 
Template Method
Compare Groupings 
Creational Structural Behavioral 
Shim Abstract Factory 
Object Pool 
Prototype 
Flyweight Iterator 
Null Object 
Compositional Builder Adapter 
Composite 
Decorator 
Facade 
Proxy 
Interpreter 
Mediator 
Observer 
Decompositional Factory Method Bridge 
Composite 
Proxy 
Chain Of Responsibility 
Command 
Mediator 
Memento 
Observer 
Strategy 
Template Method
Let’s Examine 5 Patterns 
More Closely
Compare Groupings 
Creational Structural Behavioral 
Shim Abstract Factory 
Object Pool 
Prototype 
Flyweight Iterator 
Null Object 
Compositional Builder Adapter 
Composite 
Decorator 
Facade 
Proxy 
Interpreter 
Mediator 
Observer 
Decompositional Factory Method Bridge 
Composite 
Proxy 
Chain Of Responsibility 
Command 
Mediator 
Memento 
Observer 
Strategy 
Template Method
Adapter 
use PsrLogLoggerInterface as Log 
class 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 
Existing 
Code 
Other 
Code 
“Pattern” 
Code Class 1 
Class 2 
Class 3 
Class n 
API
Adapter vs Facade 
Class 1 
Class 2 
Class 3 
Class n 
Class 1 
Class m 
Existing 
Code 
Other 
Code 
“Pattern” 
Code Class 1 
Class 2 
Class 3 
Class n 
API API Exists? Adapter
Adapter vs Facade 
Class 1 
Class 2 
Class 3 
Class n 
Class 1 
Class m 
Existing 
Code 
Other 
Code 
“Pattern” 
Code Class 1 
Class 2 
Class 3 
Class n 
API API Exists? Adapter 
New API? Facade
They Look The 
Same!!!???
Adapter Facade 
Bridge Decorator Proxy
They All Look The 
Same!!!???
Adapter Facade 
Bridge Decorator Proxy
Adapter Facade 
The Code Is The Same 
The Why Is Different 
Bridge Decorator Proxy
DeDuplicated Groupings 
Creational Structural Behavioral 
Compositional Adapter 
Composite 
Mediator 
Observer 
Decompositional Adapter 
Composite 
Command 
Mediator 
Memento 
Observer
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
A message 
$msg
Logic 
$obj
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
Ask 
$msg = $obj->something(); 
Logic Hybrid Message 
No Yes Yes
Tell 
$obj->something($msg); 
Logic Hybrid Message 
No Yes Yes
Translate 
$msg2 = $obj->do($msg); 
Logic Hybrid Message 
Yes Yes No
Everything Else Is A 
Combination Of These 
Atoms
Note:
Note: 
Ask Is Always Stateful
Note: 
Ask Is Always Stateful 
Tell Is Always Stateful
Note: 
Ask Is Always Stateful 
Tell Is Always Stateful 
Translate Can Be Stateless
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 Patterns 
Are Important! 
And Should Be Used!
But OOP Is Not About 
“Finding The Correct 
Pattern”
Focus on: 
Communication 
Interaction 
And Abstraction
Remember: 
OOP Is About 
Solving Problems
Anthony Ferrara 
joind.in/11796 
@ircmaxell 
me@ircmaxell.com 
blog.ircmaxell.com 
github.com/ircmaxell 
youtube.com/ircmaxell

Beyond design patterns phpnw14

  • 1.
    Beyond Design Patterns A Guide To Better OOP
  • 2.
    OR: Why Learning Object-Oriented Design From Design Patterns Is Bad
  • 3.
    Object Oriented Design … is the process of planning a system of interacting objects for the purpose of solving a software problem. -Wikipedia
  • 4.
    What Are Design Patterns?
  • 5.
    What Are Design Patterns? ● Commonly Occurring Solutions?
  • 6.
    What Are Design Patterns? ● Commonly Occurring Solutions? ● Commonly Occurring Structures?
  • 7.
    What Are Design Patterns? ● Commonly Occurring Solutions? ● Commonly Occurring Structures? ● Commonly Occurring Limitations?
  • 8.
    What Are Design Patterns? ● Commonly Occurring Solutions? ● Commonly Occurring Structures? ● Commonly Occurring Limitations? ● All Of The Above?
  • 9.
    What Are Design Patterns? All Of The Above!
  • 10.
    What Kind OfDesign Patterns Are There?
  • 11.
    Gang-Of-Four Design Patterns Creational Abstract Factory Builder Factory Method Object Pool Prototype
  • 12.
    Gang-Of-Four Design Patterns Creational Structural Abstract Factory Builder Factory Method Object Pool Prototype Adapter Bridge Composite Decorator Facade Flyweight Proxy
  • 13.
    Gang-Of-Four Design Patterns Creational Structural Behavioral Abstract Factory Builder Factory Method Object Pool Prototype Adapter Bridge Composite Decorator Facade Flyweight Proxy Chain Of Responsibility Command Interpreter Iterator Mediator Memento Null Object Observer Strategy Template Method
  • 14.
    Let’s Try ADifferent Organization
  • 15.
    Gang-Of-Four Design Patterns Shim Flyweight Iterator Null Object Object Pool Prototype Re-Grouped
  • 16.
    Gang-Of-Four Design Patterns Re-Grouped Shim Compositional Flyweight Iterator Null Object Object Pool Prototype Adapter Builder Decorator Facade Interpreter Mediator Observer Proxy
  • 17.
    Gang-Of-Four Design Patterns Re-Grouped Shim Compositional Decompositional Flyweight Iterator Null Object Object Pool Prototype Adapter Builder Decorator Facade Interpreter Mediator Observer Proxy Abstract Factory Bridge Chain Of Responsibility Command Factory Method Mediator Memento Observer Proxy Strategy Template Method
  • 18.
    Compare Groupings CreationalStructural Behavioral Shim Abstract Factory Object Pool Prototype Flyweight Iterator Null Object Compositional Builder Adapter Composite Decorator Facade Proxy Interpreter Mediator Observer Decompositional Factory Method Bridge Composite Proxy Chain Of Responsibility Command Mediator Memento Observer Strategy Template Method
  • 19.
    Let’s Examine 5Patterns More Closely
  • 20.
    Compare Groupings CreationalStructural Behavioral Shim Abstract Factory Object Pool Prototype Flyweight Iterator Null Object Compositional Builder Adapter Composite Decorator Facade Proxy Interpreter Mediator Observer Decompositional Factory Method Bridge Composite Proxy Chain Of Responsibility Command Mediator Memento Observer Strategy Template Method
  • 21.
    Adapter use PsrLogLoggerInterfaceas Log class PSR3Logger implements Log { public function log($level, $msg, array $ctx = array()) { $severity = $this->convertLevelToSeverity($level); watchdog(“unknown”, $msg, $ctx, $severity); } /* ... */ }
  • 22.
    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); } } /* ... */ }
  • 23.
    Adapter vs Facade Class 1 Class 2 Class 3 Class n Class 1 Class m Existing Code Other Code “Pattern” Code Class 1 Class 2 Class 3 Class n API
  • 24.
    Adapter vs Facade Class 1 Class 2 Class 3 Class n Class 1 Class m Existing Code Other Code “Pattern” Code Class 1 Class 2 Class 3 Class n API API Exists? Adapter
  • 25.
    Adapter vs Facade Class 1 Class 2 Class 3 Class n Class 1 Class m Existing Code Other Code “Pattern” Code Class 1 Class 2 Class 3 Class n API API Exists? Adapter New API? Facade
  • 26.
    They Look The Same!!!???
  • 27.
    Adapter Facade BridgeDecorator Proxy
  • 28.
    They All LookThe Same!!!???
  • 29.
    Adapter Facade BridgeDecorator Proxy
  • 30.
    Adapter Facade TheCode Is The Same The Why Is Different Bridge Decorator Proxy
  • 31.
    DeDuplicated Groupings CreationalStructural Behavioral Compositional Adapter Composite Mediator Observer Decompositional Adapter Composite Command Mediator Memento Observer
  • 32.
    DeDuplicated Re-Groupings Multiple Systems? Single System? Single Objects? Information Flow? Mediator Command Observer Structure Adapter Composite Memento
  • 33.
    DeDeDuplicated Re-Groupings Pattern Information Flow? Mediator Structure Adapter
  • 34.
    DeDeDuplicated ReRe-Groupings Pattern Information Flow? Mediator
  • 35.
    All Design PatternsDo The Same Thing:
  • 36.
    All Design PatternsDo The Same Thing: Control Information Flow
  • 37.
    So Let’s TalkAbout Information Flow
  • 38.
    So Let’s TalkAbout Communication
  • 39.
  • 40.
  • 41.
  • 42.
    Message Vs Logic Messages State
  • 43.
    Message Vs Logic Messages State Logic
  • 44.
    Message Vs Logic Messages State Logic HERE BE DRAGONS
  • 45.
    Why Break ThemApart? Logic Hybrid Message Purpose Behavior General Code State State Stateless Stateful Stateful Paradigm Functional OOP? Data
  • 46.
    Ask $msg =$obj->something(); Logic Hybrid Message No Yes Yes
  • 47.
    Tell $obj->something($msg); LogicHybrid Message No Yes Yes
  • 48.
    Translate $msg2 =$obj->do($msg); Logic Hybrid Message Yes Yes No
  • 49.
    Everything Else IsA Combination Of These Atoms
  • 50.
  • 51.
    Note: Ask IsAlways Stateful
  • 52.
    Note: Ask IsAlways Stateful Tell Is Always Stateful
  • 53.
    Note: Ask IsAlways Stateful Tell Is Always Stateful Translate Can Be Stateless
  • 54.
  • 55.
    It Lets UsAnalyze Object Roles
  • 56.
    Object Role Patterns State? Logic? Mode
  • 57.
    Object Role Patterns State? Logic? Mode Representer User No Ask/Tell
  • 58.
    Representer class User{ public function getName(); public function isAdmin(); public function setName($name); }
  • 59.
    Object Role Patterns State? Logic? Mode Representer User No Ask/Tell Doer No Yes Tell
  • 60.
    Doer class EmailSystem{ /** * @return boolean */ public function send(Message $msg); }
  • 61.
    Object Role Patterns State? Logic? Mode Representer User No Ask/Tell Doer No Yes Tell Dispatcher System No Translate
  • 62.
    Dispatcher class Controller{ /** * @return Response */ public function handle(Request $req); }
  • 63.
    Object Role Patterns State? Logic? Mode Representer User No Ask/Tell Doer No Yes Tell Dispatcher System No Translate Translator No Yes Translate
  • 64.
    Translator class UserView{ /** * @return string HTML */ public function render(User $user); }
  • 65.
    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
  • 66.
    Makers class UserFactory{ /** * @return User a user object */ public function getUser(); }
  • 67.
    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
  • 68.
    Object Role Patterns Representer Doer Dispatcher Translator Maker
  • 69.
    Object Oriented Design … is the process of planning a system of interacting objects for the purpose of solving a software problem. -Wikipedia
  • 70.
    Object Oriented Design … is the process of planning a system of interacting objects for the purpose of solving a software problem. -Wikipedia
  • 71.
    Design Patterns AreImportant! And Should Be Used!
  • 72.
    But OOP IsNot About “Finding The Correct Pattern”
  • 73.
    Focus on: Communication Interaction And Abstraction
  • 74.
    Remember: OOP IsAbout Solving Problems
  • 75.
    Anthony Ferrara joind.in/11796 @ircmaxell me@ircmaxell.com blog.ircmaxell.com github.com/ircmaxell youtube.com/ircmaxell