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 #PHPNW14
21. 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);
}
/* ... */
}
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
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
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