Beyond MVC - Enterprise PHP Patterns

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    7 Favorites

    Beyond MVC - Enterprise PHP Patterns - Presentation Transcript

    1. Beyond MVC - Enterprise PHP Patterns
      • Stefan Priebsch, e-novative GmbH
    2. „ Every pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.“ (Christopher Alexander)
    3. Patterns Are
      • Blueprints to solve common problems
      • No ready-made implementations
      • Have to be adapted to the problem
      • Are no magic bullets
    4. Some Basic Patterns
      • Model-View-Controller (MVC)
      • Singleton
      • Factory
      • Iterator
    5. Singleton
    6. Singleton
      • class Singleton { protected static $instance; private function __construct() {} public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new Singleton(); } return self::$instance; } } $singleton = Singleton::getInstance();
    7. Factory
    8. Factory
      • public function fooFactory() { return new Foo(); }
    9. Factory With Testmode
      • public function fooFactory() { if (TESTMODE) { return new FooTest(); } else { return new Foo(); } }
    10. Map And Registry
    11. Map And Registry
      • Store data in a central location
      • Make data available across layers
      • Usually a Singleton
      • e.g. for Application Configuration
    12. Map
      • class Map { protected $map = array(); public function set($aKey, $aValue) { $this->map[$aKey] = $aValue; } public function get($aKey) { return $this->map[$aKey]; } }
    13. Registry
      • class Registry { protected $foo; public function setFoo($aFoo) { $this->foo = $aFoo; } public function getFoo() { return $this->foo; } }
    14. Registry
      • class Registry { protected $foo; public function setFoo($aFoo) { $this->foo = $aFoo; } public function getFoo() { return $this->foo; } public function hasFoo() { return !is_null($this->foo); } }
    15. RequestHelper
    16. RequestHelper
      • Singleton Registry
      • Stores input data
      • Decouples application from superglobals
      • Centralized filtering/sanitation
      • Makes testing easier
    17. RequestHelper
      • class Registry { protected $get = array(); protected $post = array(); protected $cookies = array(); public function __construct($aGet, $aPost, $aCookies) { $this->get = $aGet; $this->post = $aPost; $this->cookies = $aCookies; } public function getGetValue($aKey) public function getPostValue($aKey) public function getCookieValue($aName) public function hasCookie($aName) }
    18. Filtering RequestHelper
      • class Registry { protected $get = array(); protected $post = array(); protected $cookies = array(); public function __construct($aGet, $aPost, $aCookies) { $this->get = $this->filter($aGet); $this->post = $this->filter($aPost); $this->cookies = $aCookies; } public function getGetValue($aKey) public function getPostValue($aKey) public function getCookieValue($aName) public function hasCookie($aName) }
    19. Command
    20. Command
      • Encapsulates a task
      • Selects a view
      • Allows command chaining
      • Support undoable commands
    21. Command
      • class Command { public function execute() }
    22. Validating Command
      • class Command { protected function validate() protected function doExecute() public function execute() { if ($this->validate()) { $this->doExecute(); } } }
    23. Command Selecting View
      • class Command { protected $view; public function execute() { if ($this->validate()) { $this->doExecute(); $this->view = new SuccessView(); } else { $this->view = new ErrorView(); } return $this->view->display(); } }
    24. Chained Command
      • class ChainedCommand { protected function doExecute() { $cmd = new FirstCommand(); $cmd->execute(); $cmd = new SecondCommand(); $cmd->execute(); $cmd = new ThirdCommand(); $cmd->execute(); } }
    25. Domain Model
    26. Domain Model
      • Business objects encapsulating data and behaviour
      • Similar to E/R model, but with methods and inheritance
      • Keep business logic separate from data access and presentation
    27. Domain Model
      • class Speaker { public function addPresentation($aPresentation) public function getPresentations() public function hasPresentation($aPresentation) } class Presentation { public function setSpeaker($aSpeaker) public function getSpeaker() public function addTrack($aTrack) } class Track { }
    28. Domain Model
      • class Speaker { public function addPresentation($aPresentation) public function getPresentations() public function hasPresentation($aPresentation) } class Presentation { public function addSpeaker($aSpeaker) public function getSpeakers() public function addTrack($aTrack) } class Track { }
    29. Domain Model
      • class Presentation { public function addSpeaker($aSpeaker) public function getSpeakers() public function addTrack($aTrack) } class Track { public function addPresentation($aPresentation) }
    30. Domain Model
      • class Presentation { public function addSpeaker($aSpeaker) public function getSpeakers() public function addTrack($aTrack) { $this->track = $aTrack; $aTrack->addPresentation($this); } } class Track { public function addPresentation($aPresentation) }
    31. Domain Model
      • class Speaker { public function addPresentation($aPresentation) public function getPresentations() public function hasPresentation($aPresentation) public function startSpeaking() } class Presentation { public function setSpeaker($aSpeaker) public function getSpeaker() public function addTrack($aTrack) public function startPresentation($aTime) public function endPresentation($aTime) }
    32. Front Controller
    33. Front Controller
      • Central entry point
      • Map URL to command
      • multiple Front Controllers:
        • Debug
        • Test
        • Production
      • Alternative: Page Controller
    34. Front Controller
      • class FrontController { public function __construct(RequestHelper $aRequestHelper) { $this->requestHelper = $aRequestHelper; } public function execute() { $cmd = $this->requestHelper->get('command'); $command = $this->mapCommand($cmd); print $command->execute(); } }
    35. Front Controller
      • class FrontController { public function __construct(RequestHelper $aRequestHelper) { $this->requestHelper = $aRequestHelper; $this->commandMap = new CommandMap(); } protected function mapCommand($aCommand) { return $this->commandMap->mapCommand($aCommand); } }
    36. Command Map
      • class CommandMap { protected $map = array('login' => 'LoginCommand'); public function mapCommand($aCommand) { if (!isset($this->map[$aCommand])) return new NoCommand(); return new $this->map[$aCommand]; } }
    37. Initializing the Application
      • class FrontController { protected function initApplication() public function execute() { $this->initApplication(); ... } }
    38. Applying Filters
      • class FrontController { protected $filters = array(); public function addFilter(Filter $aFilter) public function execute() { ... $result = $command->execute(); foreach($this->filters as $filter) { $result = $filter->filter($result); } print $result; } }
    39. Data Mapper
    40. Data Mapper
      • Transfers data between database and domain objects
      • Separate relational mapping from domain objects
      • Decouple domain objects from database
    41. Data Mapper
      • class SpeakerMapper { public static function load($aId) { ... $speaker = new Speaker($aId); $speaker->setValues(...); return $speaker; } public static function findByLastname($aName) protected function insert($aSpeaker) protected function update($aSpeaker) protected function delete($aId) }
    42. Lazy Load
    43. Lazy Load
      • Delay object loading until object is really needed
      • „ Placeholder“ object
      • Problem: collections and ripple loading
    44. Literature
    45. Literature
      • Fowler: „Patterns of Enterprise Application Architecture“
      • Joshua Kerievsky: „Refactoring to Patterns“
      • Matt Zandstra: „PHP 5 Objects, Patterns, and Practise“
    46. Shameless Plug
    47.  
    48.  
    49. Thank you.
    50. http://www.priebsch.de (de) http://inside.e-novative.de (en) [email_address]
    51.  

    + Stefan PriebschStefan Priebsch, 2 years ago

    custom

    3551 views, 7 favs, 9 embeds more stats

    DLW/IPC 2008 Karlsruhe Conference Presentation

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 3551
      • 3311 on SlideShare
      • 240 from embeds
    • Comments 0
    • Favorites 7
    • Downloads 130
    Most viewed embeds
    • 142 views on http://inside.e-novative.de
    • 46 views on http://www.planet-php.net
    • 17 views on http://www.priebsch.de
    • 13 views on http://webcode.lemme.at
    • 12 views on http://www.planet-php.org

    more

    All embeds
    • 142 views on http://inside.e-novative.de
    • 46 views on http://www.planet-php.net
    • 17 views on http://www.priebsch.de
    • 13 views on http://webcode.lemme.at
    • 12 views on http://www.planet-php.org
    • 5 views on http://planet-php.org
    • 3 views on http://static.slideshare.net
    • 1 views on http://lj-toys.com
    • 1 views on http://64.233.179.104

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories