OOP- aber richtig

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

    2 Groups & 2 Events

    OOP- aber richtig - Presentation Transcript

    1. OOP - aber richtig Stefan Priebsch, thePHP.cc IPC Spring 2009, Berlin
    2. Stefan Priebsch Co-Founder and Principal Consultant
    3. Disclaimer
    4. „Zum Henker mit den Regeln! Das sind sowieso eher Richtlinien ...“ --Elizabeth Swann, Pirates of the Caribbean
    5. Ist OOP langsam?
    6. foo() 3.09 µsec Test::foo() 3.26 µsec $test->foo() 3.12 µsec $test = new Test(); $test->foo() 4.03 µsec Die Benchmarks liefern auf jedem System andere Ergebnisse.
    7. 25% langsamer!
    8. 1 µsec im Verhältnis
    9. print ~ 10 µsec file_get_contents() ~ 30 µsec mysql_connect() ~ 100 µsec HTTP GET Request ~ 35,000 µsec Die Benchmarks liefern auf jedem System andere Ergebnisse.
    10. Achten Sie nicht #1 auf Performance * *Ausnahmen bestätigen die Regel
    11. Benutzen Sie ruhig Objekte
    12. class Something { protected $status = array(); public function run(...) { if (...) { … $this->status['fail'] = $e->getMessage(); } else { $this->status['skip'] = 'File is unavailable'; } return $this->status; } }
    13. class Something { protected $status = array(); public function run(...) { if (...) { … return new FailResult($e->getMessage()); } else { return new SkipResult('File is unavailable'); } } }
    14. class Result { protected $message; public function __construct($message) { $this->message = $message; } } class FailResult extends Result {} class SkipResult extends Result {}
    15. Wie verdienen Sie Ihren Lebensunterhalt?
    16. class Something { public function doManyThings() { … load data … … perform calculations … … render HTML output … … store calculation result ... } }
    17. class Something { public function doManyThings() { $this->loadData(); $this->performCalculations(); $this->renderHtml(); $this->storeResult(); } protected function loadData() protected function performCalulations() protected function renderHtml() protected function storeResult() }
    18. class DataLoader { public function loadData() } class Calculator { public function calculateResult() } class HtmlRenderer { public function render() } class StorageManager { public function storeResult() }
    19. Eine Klasse Eine Verantwortlichkeit
    20. Unterschiedliche #2 Belange trennen
    21. class DomainObject { protected function loadData() { $this->data = $this->db->query(...); } public function render() { $this->loadData(); return HtmlRenderer::createTable($this->data); } }
    22. Die anderen arbeiten lassen
    23. class DomainObject { protected function loadData() { $this->data = $this->dataManager->query(...); } public function render() { $this->loadData(); $this->dataRenderer->renderTable($this->data); } }
    24. Geheimnisse haben
    25. class SomeDomainObject { public function doSomething($a, $b, $c) { … return new SomeResult($x, $y, $z); } }
    26. Objekte dumm halten
    27. class SomeDataProcessor { public function process($filename) { … $data = file_get_contents($filename); … } }
    28. class SomeDataProcessor { public function process($data) { … } }
    29. Nicht mit Fremden sprechen
    30. class MeMySelfAndI { protected function talk() { Stranger::askForACigarette(); } }
    31. class MeMySelfAndI { protected function talk() { Stranger::getInstance()->askForACigarette(); } }
    32. class MeMySelfAndI { protected function talkTo($friend) { $friend->askForACigarette(); } } class Friend { public function askForACigarette() { return new Cigarette(); } }
    33. Lose gekoppelte #3 Klassen erzeugen
    34. Welche Werkzeuge brauchen Sie, um die Arbeit zu erledigen?
    35. class DomainObject { protected function loadData() { $this->data = $this->dataManager->query(...); } public function render() { $this->loadData(); $this->dataRenderer->renderTable($this->data); } }
    36. class DomainObject { protected $dataManager; protected $dataRenderer; public function __construct($dataManager, $dataRenderer) { $this->dataManager = $dataManager; $this->dataRenderer = $dataRenderer; } protected function loadData() public function render() }
    37. Injizieren Sie #4 Abhängigkeiten
    38. Use Dependency #4 Injection
    39. class Car { protected $engine; protected $wheels = array(); public function __construct() { $this->engine = new Engine(); $this->wheels = array( new Wheel(), new Wheel(), new Wheel(), new Wheel(), ); } }
    40. class Car { protected $engine; protected $wheels = array(); public function __construct($engine, array $wheels) { $this->engine = $engine; $this->wheels = $wheels; } }
    41. class Car { protected $engine; protected $wheels = array(); public function __construct(Engine $engine, array $wheels) { $this->engine = $engine; $this->wheels = $wheels; } }
    42. class Car { protected $engine; protected $wheels = array(); public function __construct(Engine $engine) { $this->engine = $engine; } public function addWheel(Wheel $wheel) { $this->wheels[] = $wheel; } }
    43. Komposition statt #5 Vererbung
    44. Guter OOP-Code kann immer erweitert werden
    45. #6 Das Problem lösen, nicht generalisieren
    46. Versuchen Sie #7 testgetriebene Entwicklung
    47. Kein Test, kein Feature
    48. Testgetriebene Entwicklung ● Schreiben Sie zuerst einen Unit Test ● Stellen Sie sicher, dass er fehlschlägt ● Schreiben Sie Code, bis der Test funktioniert ● Fangen Sie wieder von vorne an
    49. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { }
    50. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function test() { } }
    51. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function test...() { } }
    52. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function testSomething() { } }
    53. Auf die API fokussieren
    54. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function testAnElePHPantIsBlue() { $e = new ElePHPant(); } }
    55. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function testAnElePHPantIsBlue() { $e = new ElePHPant(); $this->assertEquals('#009', $e->getColor()); } }
    56. Woher soll ich wissen, was ein ElePHPant ist?
    57. class ElePHPant { protected $color = '#009'; public function getColor() { return $this->color; } }
    58. Du hast gerade einen blauen ElePHPanten geschaffen!
    59. class ElePHPant { protected $color = '#008'; public function getColor() { return $this->color; } }
    60. Tut mir leid, aber dieser ElePHPant ist nicht blau!
    61. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function testAnElePHPantIsBlue() { $e = new ElePHPant(); $this->assertEquals('#009', $e->getColor()); } public function testAnElePHPantHasPhpLogo() { $e = new ElePHPant(); $this->assertTrue($e->getLogo() instanceOf Logo); } }
    62. class ElePHPant { protected $color = '#009'; public function getColor() { return $this->color; } public function getLogo() { return $this->logo; } }
    63. Also, irgendwie hat dieser ElePHPant kein Logo ...
    64. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function testAnElePHPantIsBlue() { $e = new ElePHPant(); $this->assertEquals('#009', $e->getColor()); } public function testAnElePHPantHasPhpLogo() { $e = new ElePHPant(); $this->assertTrue($e->getLogo() instanceOf Logo); } }
    65. class ElePHPant { protected $color = '#009'; protected $logo; public function __construct(Logo $logo) { $this->logo = $logo; } public function getColor() { return $this->color; } public function getLogo() { return $this->logo; } }
    66. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function testAnElePHPantIsBlue() { $e = new ElePHPant(); $this->assertEquals('#009', $e->getColor()); } public function testAnElePHPantHasPhpLogo() { $e = new ElePHPant(); $this->assertTrue($e->getLogo() instanceOf Logo); } }
    67. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function testAnElePHPantIsBlue() { $e = new ElePHPant(); $this->assertEquals('#009', $e->getColor()); } public function testAnElePHPantHasPhpLogo() { $e = new ElePHPant(new Logo()); $this->assertTrue($e->getLogo() instanceOf Logo); } }
    68. require 'PHPUnit/Framework.php'; class TryingTddTest extends PHPUnit_Framework_TestCase { public function testAnElePHPantIsBlue() { $e = new ElePHPant(); $this->assertEquals('#009', $e->getColor()); } public function testAnElePHPantHasPhpLogo() { $e = new ElePHPant(new Logo()); $this->assertTrue($e->getLogo() instanceOf Logo); $this->assertEquals('PHP', $e->getLogo()->getText()); } }
    69. class Logo { protected $text; public function __construct($text) { $this->text = $text; } }
    70. class Logo { protected $text; public function __construct($text) { $this->text = $text; } ... }
    71. … und Sie programmieren glücklich und zufrieden bis ans Ende aller Tage ...
    72. http://thephp.cc Twitter: thephpcc stefan@thephp.cc http://www.priebsch.de Twitter: spriebsch Xing, LinkedIn, Facebook

    + Stefan PriebschStefan Priebsch, 5 months ago

    custom

    695 views, 0 favs, 1 embeds more stats

    "Ein bißchen OOP" kann heute jeder, schließlich s more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 695
      • 693 on SlideShare
      • 2 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 7
    Most viewed embeds
    • 2 views on http://lamp.jiecn.net

    more

    All embeds
    • 2 views on http://lamp.jiecn.net

    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

    Tags