design patterns as power of programing

Download Design patterns as power of programing

If you can't read please download the document

Upload: lukas-lesniewski

Post on 10-May-2015

194 views

Category:

Engineering


0 download

TRANSCRIPT

  • 1.Design Patterns as power of programming Lukas Lesniewski

2. About me PHP Programmer Experience with Zend Framework 1 and 2 Speaker at Joomla! Day 2013 Poland 3. Power of Open Source: PHP PHP 24% PHP as language Java PHP Python Ruby .NET Perl ASP PHP 75% Servers usage Java PHP Python Ruby .NET Perl ColdFusion 4. PHP Frameworks Zend CakePHP Symfony YiiWACT Prado Akelos CodeIgniter Joomla Wordpress Laravel Phalcon and more! 5. Frameworks features Inversion controll Default Behavior Extensibility Non-moditiable 6. Close & Open plugin2 plugin3 plugin1 Shop 7. tekst How to use it? Time to learn patterns! 8. Singleton Programmer 9. Singleton class SimpleSingleton { private static $_instance = null; private function __constructor() {} public static function getInstance() { if (!static::$_instance) static::$_instance = new SimpleSingleton(); return static::$_instance; } } // $bad = new SimpleSingleton(); $good = SimpleSingleton::getInstance(); JApplicationCms 10. Registry class MyRegistry { private static $_data = array(); public static function setItem($key, $value) { static::$_data[$key] = $value; } public static function getItem($key) { return static::$_data[$key]; } public static function hasItem($key) { return array_key_exists($key, static::$_data); } public static function getItems() { return static::$_data; } } Register::setItem("key1", new stdClass); Register::setItem("key2", 2); JRegistry 11. Dependency injectionProgrammer Interface Interface Interface Interface Interface 12. DI interface ComputerInterface { public function start(); public function shutdown(); public function hideMyFavoritesVideosFolder(); public function cleanBrowserHistory(); public function getJoomla(); } class LinuxComputer implements ComputerInterface {} class MacComputer implements ComputerInterface {} class WinComputer implements ComputerInterface {} Container 13. DI class Programmer { private $computer; public function setComputer(ComputerInterface $c) { $this->computer = $c; } public function __construct(ComputerInterface $c) { $this->setComputer($c); } public function code() { if ($this->computer->getJoomla() == NULL) { throw new Exception('Impossible!', 1); } return 'You're so lucky!'; } } $c = new LinuxComputer(); $p = new Programmer($c); $p->code(); Container 14. Factory abstract class Programmer { abstract public function code(); private $computer; public function setComputer(ComputerInterface $c) { $this->computer = $c; } } class JavaProgrammer extends Programmer { public function code() { echo 'I use Java 8 on my' . $this->computer; } } class PHPProgrammer extends Programmer { public function code() { echo 'I use Joomla on my' . $this->computer; } } class CPPProgrammer extends Programmer { public function code() { echo 'I use c++ on my' . $this->computer; } } JFractory 15. Factory class ProgrammerFactory { public static function createProgrammer($lang, $computer) { $p; switch ($lang) { case 'JAVA': $p = new JavaProgrammer(); break; case 'PHP': $p = new PHPProgrammer(); break; case 'C++': $p = new CPPProgrammer(); break; default: $p = new PHPProgrammer(); break; } switch ($computer) { default: $p->setComputer(new LinuxComputer); break; } return $p; } } JFractory 16. JoomlaSiteWithMyPlugin WpSiteWithMyPlugin DrupalSiteWithMyPlugin JoomlaSiteWithTemplate JoomlaSite DrupalSiteWithTemplate WpSiteDrupalSite WpSiteWithTemplate Site 17. Template getPrice() Plugin getPrice() DecoratorJoomlaSite getPrice() 18. Decorator abstract class Site { abstract public function getPrice(); private $desc = "There is no description"; public function getDesc() { return $desc; } } class WordpressSite extends Site { private $desc = "This is a Wordpress site"; public function getPrice() { return 99; } } class JoomlaSite extends Site { private $desc = "This is a Joomla Site"; public function getPrice() { return 99; } } class DrupalSite extends Site { private $desc = "This is a Drupal Site"; public function getPrice() { return 99; } } 19. Decorator class AmazingTemplate extends Site { private $site; public function __construct (Site $site) { $this->site = $site; } public function getPrice() { return $this->site->getPrice() + 199; } } class MyPlugin extends Site { private $site; public function __construct (Site $site) { $this->site = $site; } public function getPrice() { return $this->site->getPrice() + 49; } } $site = new JoomlaSite(); $site = new AmazingTemplate($site); $site = new MyPlugin($site); $site = new MyPlugin($site); 20. Template return 199 Plugin return 49 JoomlaSite return 99 21. Steve Jobs When you first start off trying to solve a problem, the first solutions you come up with are very complex, and most people stop there. But if you keep going, and live with the problem and peel more layers of the onion off, you can often times arrive at some very elegant and simple solutions. 22. facebook.com/LLUKAS.DE 23. Sources http://en.wikipedia.org/wiki/Software_framework http://blog.websitesframeworks.com/2013/03/program ming-language-statistics-in-server-side-161/ http://langpop.com/