Better than Ad-hoc                           How to Make Things Suck (Less)Monday, August 13, 2012
Monday, August 13, 2012
SAURON, THE DARK LORDMonday, August 13, 2012
I’ll leave what happens         next to the imagination.Monday, August 13, 2012
Monday, August 13, 2012
Seriously though...Monday, August 13, 2012
What are patterns?Monday, August 13, 2012
- Repeatable solutions to common design problemsMonday, August 13, 2012
- Repeatable solutions to common design problems    - Standardized namesMonday, August 13, 2012
- Repeatable solutions to common design problems    - Standardized names    - Essential for framework and library sustaina...
- Repeatable solutions to common design problems    - Standardized names    - Essential for framework and library sustaina...
- Repeatable solutions to common design problems    - Standardized names    - Essential for framework and library sustaina...
- Repeatable solutions to common design problems    - Standardized names    - Essential for framework and library sustaina...
- Repeatable solutions to common design problems    - Standardized names    - Essential for framework and library sustaina...
Classical PatternsMonday, August 13, 2012
Classical Patterns    - First real work done in the 1980sMonday, August 13, 2012
Classical Patterns    - First real work done in the 1980s    - GOF released in 1994 (“Design Patterns, Elements of    Reus...
Classical Patterns    - First real work done in the 1980s    - GOF released in 1994 (“Design Patterns, Elements of    Reus...
Classical Patterns    - First real work done in the 1980s    - GOF released in 1994 (“Design Patterns, Elements of    Reus...
Monday, August 13, 2012
Quick OverviewMonday, August 13, 2012
Quick Overview    - Creational patterns deal with how objects are createdMonday, August 13, 2012
Quick Overview    - Creational patterns deal with how objects are created    - Structural patterns deal with how objects a...
Quick Overview    - Creational patterns deal with how objects are created    - Structural patterns deal with how objects a...
Creational PatternsMonday, August 13, 2012
abstract class AbstractFactory()       {         protected function shared(){...//shared}           abstract public functi...
class FactoryMethod()       {         public function __construct(){...//shared}           public function __call($class, ...
class ObjectPool()       {         public $register = array(            ‘Foo’,            ‘Bar’         );           publi...
class Singleton()       {         public $instance = false;           private function __construct($register = false){...}...
class Multiton()       {         public $instances = array();           private function __construct(){...}           publ...
class Director()       {         public function __construct(){...}           public function build($obj){             ......
class ConcretePrototype()       {         public $obj = false;           public $baz = “something cool”           public f...
Structural PatternsMonday, August 13, 2012
class Foo()       {         public function __construct(){}       }       class FooAdapter()       {         public static...
interface BridgeImplementation(){         public function doMethod($args);       }       class ConcreteA implements Bridge...
class Compositte       {         public $nodes = array();            public $properties = array();            public funct...
class Facade       {         public static function simpleMethod(){           $foo = HiddenByFacadeA::foo();           $ba...
class RemoteProxy       {         private static function request($method, $args){           ../maybe some curl stuff here...
class Filter       {         public static function applyOne($data, $filter){           return ( $filter instanceof Closure ...
Behavioral PatternsMonday, August 13, 2012
class ChainOfResponsibility()       {         public $next = false;            public function setNext($chained){         ...
class ObserverA extends Observer(){...}       class ObservedFoo extends Observed()       {         public $observers = arr...
class Invoker extends Observer {...}       interface Command(){...}       class CommandA implements Command(){...}       $...
abstract class AbstractInterpreter(){...}       class InterpreterA extends AbstractInterpreter{...}       interface Expres...
interface Iterator {...}       abstract class MyIterator implements Iterator(){...}       class Util extends MyIterator(){...
class CareTaker{...}       trait Memento       {         public function recordState(){           CareTaker::recordState(_...
interface Strategy{...}       class ConcreteStrategy implements Strategy {...}       class Context       {         public ...
abstract class AbstractTemplate       {         ...//shared implementation, enforced methods       }       class ConcreteT...
Patterns on the WebMonday, August 13, 2012
Patterns on the Web    - Once upon a time, nobody was thinking about software    design patterns in web appsMonday, August...
Patterns on the Web    - Once upon a time, nobody was thinking about software    design patterns in web apps    - Desktop ...
Patterns on the Web    - Once upon a time, nobody was thinking about software    design patterns in web apps    - Desktop ...
Patterns on the Web    - Once upon a time, nobody was thinking about software    design patterns in web apps    - Desktop ...
Patterns in CodeIgniterMonday, August 13, 2012
class Blog extends CI_Controller       {         public function __construct(){...}            public function index(){   ...
class CI_Hooks {...}       $config[enable_hooks] = TRUE;       $hook[pre_controller] = array(          class => MyClass,   ...
class CI_Controller {         private static $instance;            public function __construct(){...}            public st...
class CI_Controller{...         public function __construct()         {           self::$instance =& $this;               ...
//registry       function &is_loaded($class = )       {       	 static $_is_loaded = array();       	     if ($class !== )...
function &load_class($class, $directory = libraries,       $prefix = CI_){       	 static $_classes = array();             ...
//Index.php (FrontController)       	 // Load CI core files            // Set some constants       	 // Pass on the request...
if ( ! file_exists($driver_file))       {          show_error(Invalid DB driver);       }       require_once($driver_file);  ...
Reading List           - “Design Patterns: Elements of Reusable Object-             Oriented Software” by Erich Gamma, Ric...
Reading List       - Everything on Wikipedia about Design Patterns                 - “CodeComplete 2: A practical handbook...
Me                                @calvinfroedge                          http://www.calvinfroedge.com                    ...
FinMonday, August 13, 2012
Upcoming SlideShare
Loading in...5
×

Ciconf 2012 - Better than Ad-hoc

1,687

Published on

My slides from CIConf 2012 on design patterns.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,687
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ciconf 2012 - Better than Ad-hoc

  1. 1. Better than Ad-hoc How to Make Things Suck (Less)Monday, August 13, 2012
  2. 2. Monday, August 13, 2012
  3. 3. SAURON, THE DARK LORDMonday, August 13, 2012
  4. 4. I’ll leave what happens next to the imagination.Monday, August 13, 2012
  5. 5. Monday, August 13, 2012
  6. 6. Seriously though...Monday, August 13, 2012
  7. 7. What are patterns?Monday, August 13, 2012
  8. 8. - Repeatable solutions to common design problemsMonday, August 13, 2012
  9. 9. - Repeatable solutions to common design problems - Standardized namesMonday, August 13, 2012
  10. 10. - Repeatable solutions to common design problems - Standardized names - Essential for framework and library sustainabilityMonday, August 13, 2012
  11. 11. - Repeatable solutions to common design problems - Standardized names - Essential for framework and library sustainability - Rules can be bentMonday, August 13, 2012
  12. 12. - Repeatable solutions to common design problems - Standardized names - Essential for framework and library sustainability - Rules can be bent - The result of good initial design or necessary refactoringMonday, August 13, 2012
  13. 13. - Repeatable solutions to common design problems - Standardized names - Essential for framework and library sustainability - Rules can be bent - The result of good initial design or necessary refactoring - Will change the way you think about programmingMonday, August 13, 2012
  14. 14. - Repeatable solutions to common design problems - Standardized names - Essential for framework and library sustainability - Rules can be bent - The result of good initial design or necessary refactoring - Will change the way you think about programming - Will lead the people to the promised landMonday, August 13, 2012
  15. 15. Classical PatternsMonday, August 13, 2012
  16. 16. Classical Patterns - First real work done in the 1980sMonday, August 13, 2012
  17. 17. Classical Patterns - First real work done in the 1980s - GOF released in 1994 (“Design Patterns, Elements of Reusable Object Oriented Software”)Monday, August 13, 2012
  18. 18. Classical Patterns - First real work done in the 1980s - GOF released in 1994 (“Design Patterns, Elements of Reusable Object Oriented Software”) - Much writing and evolution sinceMonday, August 13, 2012
  19. 19. Classical Patterns - First real work done in the 1980s - GOF released in 1994 (“Design Patterns, Elements of Reusable Object Oriented Software”) - Much writing and evolution since - Important, but not sacredMonday, August 13, 2012
  20. 20. Monday, August 13, 2012
  21. 21. Quick OverviewMonday, August 13, 2012
  22. 22. Quick Overview - Creational patterns deal with how objects are createdMonday, August 13, 2012
  23. 23. Quick Overview - Creational patterns deal with how objects are created - Structural patterns deal with how objects are composedMonday, August 13, 2012
  24. 24. Quick Overview - Creational patterns deal with how objects are created - Structural patterns deal with how objects are composed - Behavior patterns deal with how objects are utilizedMonday, August 13, 2012
  25. 25. Creational PatternsMonday, August 13, 2012
  26. 26. abstract class AbstractFactory() { protected function shared(){...//shared} abstract public function doSomething(){} } class ConcreteFactory extends AbstractFactory(){ public function doSomething(){..//unique} } $factory = new ConcreteFactory();Monday, August 13, 2012
  27. 27. class FactoryMethod() { public function __construct(){...//shared} public function __call($class, $args){ if ( class_exists($class) ) { return new $class($args); } throw new Exception(“$class does not exist.”); } } $factory = new FactoryMethod(); $factory->WidgetA(array(‘config’ => ‘item’));Monday, August 13, 2012
  28. 28. class ObjectPool() { public $register = array( ‘Foo’, ‘Bar’ ); public function __construct($register = false){ ...//register objects passed in constructor, build pool } } $pool = new ObjectPool(); $pool->Foo->DoSomething();Monday, August 13, 2012
  29. 29. class Singleton() { public $instance = false; private function __construct($register = false){...} public function __clone(){//throw error} public static function instance(){ if( ! self::$instance ) self::$instance = new Singleton(); return self::$instance; } } $s = Singleton::instance();Monday, August 13, 2012
  30. 30. class Multiton() { public $instances = array(); private function __construct(){...} public static function instance($name = ‘default’){ if( ! self::$instances[$name] ) self::$instances[$name] = new Multiton(); return self::$instances[$name]; } } $a = Multiton::instance(‘foo’); $b = Multiton::instance(‘bar’);Monday, August 13, 2012
  31. 31. class Director() { public function __construct(){...} public function build($obj){ ...//shared logic for manipulating object } } class ConcreteBuilder() { public function __construct(){...} } $director = new Director(); $object = $director->build(new ConcreteBuilder())Monday, August 13, 2012
  32. 32. class ConcretePrototype() { public $obj = false; public $baz = “something cool” public function __construct(){ $this->obj = new Foo(); } } $c = new ConcretePrototype(); $p = clone $c; //shallow copy $x = unserialize(serialize($p)); //deep copyMonday, August 13, 2012
  33. 33. Structural PatternsMonday, August 13, 2012
  34. 34. class Foo() { public function __construct(){} } class FooAdapter() { public static function __callStatic($method, $args){ //Requests to Foo are translated. Foo is called and response is returned. } } $response = FooAdapter::FooMethod($args);Monday, August 13, 2012
  35. 35. interface BridgeImplementation(){ public function doMethod($args); } class ConcreteA implements BridgeImplmentation(){ public function doMethod($args){...} } abstract class Foo{...} class RefinedFoo extends Foo{...} $foo = new RefinedFoo($args, new ConcreteA()); $result = $foo->doMethod($args);Monday, August 13, 2012
  36. 36. class Compositte { public $nodes = array(); public $properties = array(); public function __construct($name, $properties){...} public function node(Composite $node); } $nodeA = new Composite(array(‘bah’ => ‘test’)); $nodeB = new Composite(array(‘var’ => ‘haha’)); $nodeB->node(new Composite(array(‘baz’ => ‘bar’)); $nodeA->node($nodeB);Monday, August 13, 2012
  37. 37. class Facade { public static function simpleMethod(){ $foo = HiddenByFacadeA::foo(); $baz = HiddenByFacadeB::bar($foo); return HiddenByFacadeB::doh($baz); } } class HiddenByFacadeA{...} class HiddenByFacadeB{...} $simple = Facade::simpleMethod();Monday, August 13, 2012
  38. 38. class RemoteProxy { private static function request($method, $args){ ../maybe some curl stuff here return $remoteServiceCallResult; } public static function __callStatic($method, $args){ return self::request($method, $args); } } $result = RemoteProxy::someMethod();Monday, August 13, 2012
  39. 39. class Filter { public static function applyOne($data, $filter){ return ( $filter instanceof Closure ) ? $filter($data) : self::$filter($data); } public static function applyAll($d, array $filters){ foreach($filters as $f) $data = self::applyOne($d, $f); return $data; } ... } $r = Filter::applyAll($data, array(‘foo’, function(){...}));Monday, August 13, 2012
  40. 40. Behavioral PatternsMonday, August 13, 2012
  41. 41. class ChainOfResponsibility() { public $next = false; public function setNext($chained){ static::$next = $chained; } } class ChainableA extends ChainOfResponsibility(){...} class ChainableB extends ChainOfResponsibility(){...} class ChainableC extends ChainOfResponsibility(){...} new ChainableA()->setNext( new ChainableB()->setNext(new ChainableC()) );Monday, August 13, 2012
  42. 42. class ObserverA extends Observer(){...} class ObservedFoo extends Observed() { public $observers = array(‘ObserverA’, ‘ObserverB’); public function __call($method, $args){ ...//do something $this->notifyObservers($this->observers); } } $subject = new ObservedFoo(); $subject->doSomething();Monday, August 13, 2012
  43. 43. class Invoker extends Observer {...} interface Command(){...} class CommandA implements Command(){...} $executeLater = Invoker::schedule( “condition”, “someMethod”, new CommandA(array(‘foo’ => ‘bar’)) );Monday, August 13, 2012
  44. 44. abstract class AbstractInterpreter(){...} class InterpreterA extends AbstractInterpreter{...} interface Expression(){...} class String implements Expression{...} class Number implements Expression{...} class Command implements Expression{...} ... $interpreted = new Interpreter()->interpret (“var=doBaz.foo[tabba|too|3|a] && abacadbra[var]”)Monday, August 13, 2012
  45. 45. interface Iterator {...} abstract class MyIterator implements Iterator(){...} class Util extends MyIterator(){...} $iterator = new Util(); while( $iterator->valid() ) { $iterator->current(); //Do something with this $iterator->next(); }Monday, August 13, 2012
  46. 46. class CareTaker{...} trait Memento { public function recordState(){ CareTaker::recordState(__CLASS__, serialize($this)); } public function restoreState(){...} } class Foo { use Memento; ... }Monday, August 13, 2012
  47. 47. interface Strategy{...} class ConcreteStrategy implements Strategy {...} class Context { public $strategy = false; public function __construct($strategy){...} public function applyStrategy(){...} } $context = new Context( new ConcreteStrategy() );Monday, August 13, 2012
  48. 48. abstract class AbstractTemplate { ...//shared implementation, enforced methods } class ConcreteTemplateA extends AbstractTemplate { ..//concrete (custom) implementation } $template = new ConcreteTemplateA( );Monday, August 13, 2012
  49. 49. Patterns on the WebMonday, August 13, 2012
  50. 50. Patterns on the Web - Once upon a time, nobody was thinking about software design patterns in web appsMonday, August 13, 2012
  51. 51. Patterns on the Web - Once upon a time, nobody was thinking about software design patterns in web apps - Desktop and mobile computing now often dependent on remote (web) servicesMonday, August 13, 2012
  52. 52. Patterns on the Web - Once upon a time, nobody was thinking about software design patterns in web apps - Desktop and mobile computing now often dependent on remote (web) services - Now web apps / server apps can be wayyy complicatedMonday, August 13, 2012
  53. 53. Patterns on the Web - Once upon a time, nobody was thinking about software design patterns in web apps - Desktop and mobile computing now often dependent on remote (web) services - Now web apps / server apps can be wayyy complicated - Patterns help us apply systematic rigor to the complexityMonday, August 13, 2012
  54. 54. Patterns in CodeIgniterMonday, August 13, 2012
  55. 55. class Blog extends CI_Controller { public function __construct(){...} public function index(){ ... $this->load->view(‘blog/index’); } } class BlogModel extends CI_Model { public function __construct(){...} public function posts(){...} }Monday, August 13, 2012
  56. 56. class CI_Hooks {...} $config[enable_hooks] = TRUE; $hook[pre_controller] = array( class => MyClass, function => Myfunction, filename => Myclass.php, filepath => hooks, params => array(beer, wine, snacks) ); $EXT->call_hook(pre_controller);Monday, August 13, 2012
  57. 57. class CI_Controller { private static $instance; public function __construct(){...} public static function &get_instance(){...} } function &get_instance() { return CI_Controller::get_instance(); }Monday, August 13, 2012
  58. 58. class CI_Controller{... public function __construct() { self::$instance =& $this; foreach (is_loaded() as $var => $class) { $this->$var =& load_class($class); } $this->load =& load_class(‘Loader’, ‘core’); $this->load->initialize(); log_message(‘debug’, ‘Controller Class Initialized’); }... }Monday, August 13, 2012
  59. 59. //registry function &is_loaded($class = ) { static $_is_loaded = array(); if ($class !== ) { $_is_loaded[strtolower($class)] = $class; } return $_is_loaded; }Monday, August 13, 2012
  60. 60. function &load_class($class, $directory = libraries, $prefix = CI_){ static $_classes = array(); ... // Does the class exist? If so, were done... if (isset($_classes[$class])){ return $_classes[$class]; } ... // Keep track of what we just loaded is_loaded($class); ... $_classes[$class] = new $name(); return $_classes[$class]; }Monday, August 13, 2012
  61. 61. //Index.php (FrontController) // Load CI core files // Set some constants // Pass on the requestMonday, August 13, 2012
  62. 62. if ( ! file_exists($driver_file)) { show_error(Invalid DB driver); } require_once($driver_file); // Instantiate the DB adapter $driver = CI_DB_.$params[dbdriver]._driver; $DB = new $driver($params);Monday, August 13, 2012
  63. 63. Reading List - “Design Patterns: Elements of Reusable Object- Oriented Software” by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides - SourceMaking.com and Design Patterns: Simply - “Use of Design Patterns in PHP-based Web Application Frameworks” by Andris PaikensMonday, August 13, 2012
  64. 64. Reading List - Everything on Wikipedia about Design Patterns - “CodeComplete 2: A practical handbook of software construction” by Steve McConnel - “The Meditations” by Marcus Aurelius - “Politics and the English Language” George OrwellMonday, August 13, 2012
  65. 65. Me @calvinfroedge http://www.calvinfroedge.com github.com/calvinfroedgeMonday, August 13, 2012
  66. 66. FinMonday, August 13, 2012
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×