The book Design Patterns introduces a lot of these concepts. At the time I was still learning object orientation (OO), so I found many of the concepts in that book very hard to grasp. But as I became more and more familiar with OO concepts — especially the use of interfaces and inheritance — I began to see the practical value in design patterns. As an application developer, even if you never learn about any pattern, or how and when to use these patterns, it won’t have much of an impact on your career. But I found that after gaining a solid knowledge of these patterns and the ones introduced in the developerWorks article “Five Common PHP Design Patterns” (see Resources), you can accomplish two things:
Enabling high-bandwidth sessions If you understand design patterns, you’ll be able to build reliable OO applications faster. But when the whole development team knows the various patterns, you suddenly have very high-bandwidth sessions. You no longer need to discuss all the classes that will be used everywhere. Instead, you can talk to the others in terms of patterns. “I’m going to reference a singleton here, then use an iterator to walk the object collection, and then…” is much faster than walking through the classes, methods and interfaces that make up those patterns. The communication efficiency alone makes it worth spending time studying patterns through sessions as a team. Painful lessons reduced Every design pattern describes a proven way to solve a common problem. So you don’t need to worry about whether your design is correct, as long as you’ve chosen a pattern that provides the benefits you need. Flaws There’s a saying that goes well: “When you’re holding a hammer, everything looks like a nail.” When you think you’ve found an excellent pattern, you may try to use it everywhere, even in places where it shouldn’t be used. Remember that you must consider the purpose of the pattern you’re learning, and don’t force these patterns into every part of the application just for the sake of using them.
This article will introduce five patterns that can be used to improve PHP code. Each pattern is introduced with a specific scenario. The PHP code for these patterns is available in the Downloads section.
Requirements To get the most out of this article and to use the examples, you need the following software installed on your computer:
PHP V5 or later (this article was written using PHP V5.2.4) A compression program, such as WinZIP (for extracting the downloadable code archive) Note: although you can also use a plain text editor, I find an editor with syntax highlighting and syntax error checking really helpful. The examples in this article were written using Eclipse PHP Development Tools (PDT).
/** * 艾瑞可erik * https://erik.xyz * Class bank * Delegation pattern */ class bank { protected $info = [];
public static function load() { return new bank(); }
/** * Pass in parameters and set the basic information * @param $type * @param $money * Set the bank deposit type */ public function updateBankInfo($type, $money) { $this->info[$type] = $money; }
public function bankWithdraw($bankType) { $obj = $bankType::load(); return $obj->bankMain($this->info); } }
/** * Deposit operation * Class bankDeposit */ class bankDeposit { public static function load() { return new bankDeposit(); }
public function bankMain($data) { return $data['bankDeposit']; } }
/** * Withdrawal operation * Class bankWithdraw */ class bankWithdraw { public static function load() { return new bankWithdraw(); }
public function bankMain($data) { return $data['bankWithdraw']; } }
$bank = bank::load();
//set the data $bank->updateBankInfo("bankWithdraw", 500); $bank->updateBankInfo("bankDeposit", 100);
//payment choice interface PayTypeErik { public function addObserverErik($observer); }
class PayListErik implements PayTypeErik { private $observers = [];
public function load() { return new PayListErik(); }
public function addCustomerErik($method, $name) { if (empty($method)) { return false; } foreach ($this->observers as $obs) { $obs->$method($this, $name); } }
public function addObserverErik($observer) { $this->observers[] = $observer; } }
/** Logging * Class PayListLoggerErik */ class PayListLoggerErik implements PayLoggerErik { public function load() { return new PayListLoggerErik(); }
public function onChangeErik($sender, $args) { echo "选择成功!" . $args . PHP_EOL; }
public function setLoggerErik($sender, $args) { echo "设置成功了!" . $args. PHP_EOL; } }
<?php /** * 艾瑞可erik * https://erik.xyz * * Singleton pattern */ class SingleErik { private $props=[]; private static $instanceErik; final private function __construct(){}
//singleton method public static function getInstanceErik(){ if(empty(self::$instanceErik)){ self::$instanceErik=new SingleErik(); } return self::$instanceErik; } //singleton definition methods public function setPropertyErik($key,$val){ $this->props[$key]=$val; }
public function getPropertyErik($key){ return $this->props[$key]; }
final protected function __clone(){} }
$perf=singleErik::getInstanceErik(); $perf->setPropertyErik("blog",["title"=>"艾瑞可erik","url"=>"https://erik.xyz"]); $getData=$perf->getPropertyErik("blog"); print_r($getData); //destroy the reference and free the space unset($perf);