• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Ciconf 2012 - Better than Ad-hoc
 

Ciconf 2012 - Better than Ad-hoc

on

  • 1,899 views

My slides from CIConf 2012 on design patterns.

My slides from CIConf 2012 on design patterns.

Statistics

Views

Total Views
1,899
Views on SlideShare
1,752
Embed Views
147

Actions

Likes
0
Downloads
14
Comments
0

3 Embeds 147

http://lanyrd.com 144
https://twimg0-a.akamaihd.net 2
https://si0.twimg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Ciconf 2012 - Better than Ad-hoc Ciconf 2012 - Better than Ad-hoc Presentation Transcript

    • 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 sustainabilityMonday, August 13, 2012
    • - Repeatable solutions to common design problems - Standardized names - Essential for framework and library sustainability - Rules can be bentMonday, August 13, 2012
    • - 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
    • - 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
    • - 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
    • 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 Reusable Object Oriented Software”)Monday, August 13, 2012
    • 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
    • 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
    • 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 are composedMonday, August 13, 2012
    • 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
    • Creational PatternsMonday, August 13, 2012
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • Structural PatternsMonday, August 13, 2012
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • Behavioral PatternsMonday, August 13, 2012
    • 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
    • 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
    • class Invoker extends Observer {...} interface Command(){...} class CommandA implements Command(){...} $executeLater = Invoker::schedule( “condition”, “someMethod”, new CommandA(array(‘foo’ => ‘bar’)) );Monday, August 13, 2012
    • 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
    • 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
    • class CareTaker{...} trait Memento { public function recordState(){ CareTaker::recordState(__CLASS__, serialize($this)); } public function restoreState(){...} } class Foo { use Memento; ... }Monday, August 13, 2012
    • 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
    • abstract class AbstractTemplate { ...//shared implementation, enforced methods } class ConcreteTemplateA extends AbstractTemplate { ..//concrete (custom) implementation } $template = new ConcreteTemplateA( );Monday, August 13, 2012
    • 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 13, 2012
    • 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
    • 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
    • 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
    • Patterns in CodeIgniterMonday, August 13, 2012
    • 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
    • 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
    • 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
    • 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
    • //registry function &is_loaded($class = ) { static $_is_loaded = array(); if ($class !== ) { $_is_loaded[strtolower($class)] = $class; } return $_is_loaded; }Monday, August 13, 2012
    • 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
    • //Index.php (FrontController) // Load CI core files // Set some constants // Pass on the requestMonday, August 13, 2012
    • 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
    • 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
    • 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
    • Me @calvinfroedge http://www.calvinfroedge.com github.com/calvinfroedgeMonday, August 13, 2012
    • FinMonday, August 13, 2012