Your SlideShare is downloading. ×
0
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Ciconf 2012 - Better than Ad-hoc
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ciconf 2012 - Better than Ad-hoc

1,668

Published on

My slides from CIConf 2012 on design patterns.

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,668
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Better than Ad-hoc How to Make Things Suck (Less)Monday, August 13, 2012
  • 2. Monday, August 13, 2012
  • 3. SAURON, THE DARK LORDMonday, August 13, 2012
  • 4. I’ll leave what happens next to the imagination.Monday, August 13, 2012
  • 5. Monday, August 13, 2012
  • 6. Seriously though...Monday, August 13, 2012
  • 7. What are patterns?Monday, August 13, 2012
  • 8. - Repeatable solutions to common design problemsMonday, August 13, 2012
  • 9. - Repeatable solutions to common design problems - Standardized namesMonday, August 13, 2012
  • 10. - Repeatable solutions to common design problems - Standardized names - Essential for framework and library sustainabilityMonday, August 13, 2012
  • 11. - Repeatable solutions to common design problems - Standardized names - Essential for framework and library sustainability - Rules can be bentMonday, August 13, 2012
  • 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. - 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. - 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. Classical PatternsMonday, August 13, 2012
  • 16. Classical Patterns - First real work done in the 1980sMonday, August 13, 2012
  • 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. 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. 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. Monday, August 13, 2012
  • 21. Quick OverviewMonday, August 13, 2012
  • 22. Quick Overview - Creational patterns deal with how objects are createdMonday, August 13, 2012
  • 23. Quick Overview - Creational patterns deal with how objects are created - Structural patterns deal with how objects are composedMonday, August 13, 2012
  • 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. Creational PatternsMonday, August 13, 2012
  • 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. 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. 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. 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. 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. 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. 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. Structural PatternsMonday, August 13, 2012
  • 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. 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. 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. 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. 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. 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. Behavioral PatternsMonday, August 13, 2012
  • 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. 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. 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. 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. 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. class CareTaker{...} trait Memento { public function recordState(){ CareTaker::recordState(__CLASS__, serialize($this)); } public function restoreState(){...} } class Foo { use Memento; ... }Monday, August 13, 2012
  • 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. abstract class AbstractTemplate { ...//shared implementation, enforced methods } class ConcreteTemplateA extends AbstractTemplate { ..//concrete (custom) implementation } $template = new ConcreteTemplateA( );Monday, August 13, 2012
  • 49. Patterns on the WebMonday, August 13, 2012
  • 50. Patterns on the Web - Once upon a time, nobody was thinking about software design patterns in web appsMonday, August 13, 2012
  • 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. 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. 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. Patterns in CodeIgniterMonday, August 13, 2012
  • 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. 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. 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. 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. //registry function &is_loaded($class = ) { static $_is_loaded = array(); if ($class !== ) { $_is_loaded[strtolower($class)] = $class; } return $_is_loaded; }Monday, August 13, 2012
  • 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. //Index.php (FrontController) // Load CI core files // Set some constants // Pass on the requestMonday, August 13, 2012
  • 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. 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. 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. Me @calvinfroedge http://www.calvinfroedge.com github.com/calvinfroedgeMonday, August 13, 2012
  • 66. FinMonday, August 13, 2012

×