Bad:
- class Employee{
- public function work()
- { // ....working
- }
- }
- class Robot extends Employee{
- public function work() { //.... working much more
- }
- }
- class Manager{
- private $employee;
- public function __construct(Employee $employee)
- { $this->employee = $employee;
- } public function manage()
- { $this->employee->work();
- }
- }
Good:
别写反复代码 (DRY)
这条原则大年夜家应当都是比较熟悉了。
尽你最大年夜的尽力去避免复制代码,它是一种异常糟糕的行动,复制代码通平平易近味着当你须要变革一些逻辑时,你须要修改不止一处。
Bad:
- function showDeveloperList($developers){
- foreach ($developers as $developer) {
- $expectedSalary =
- $developer->calculateExpectedSalary();
- $experience = $developer->getExperience();
- $githubLink = $developer->getGithubLink();
- $data = [
- $expectedSalary,
- $experience,
- $githubLink
- ];
- render($data);
- }
- }
- function showManagerList($managers){
- foreach ($managers as $manager) {
- $expectedSalary =
- $manager->calculateExpectedSalary();
- $experience = $manager->getExperience();
- $githubLink = $manager->getGithubLink();
- $data = [
- $expectedSalary,
- $experience,
推荐阅读
沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散! 比来在进行微办事架构的交换和>>>详细阅读
本文标题:PHP代码简洁之道——SOLID原则
地址:http://www.17bianji.com/lsqh/38154.html
1/2 1