作家
登录

PHP代码简洁之道——SOLID原则

作者: 来源: 2017-10-25 09:04:08 阅读 我要评论

 renderLargeRectangles($rectangles){     
  •     foreach ($rectangles as $rectangle) { 
  •         $rectangle->setWidth(4); 
  •         $rectangle->setHeight(5); 
  •         $area = $rectangle->getArea(); // BAD: Will return 25 for Square. Should be 20. 
  •         $rectangle->render($area); 
  •     } 
  •  
  • $rectangles =  
  • [new Rectangle(), new Rectangle(), new Square()]; 
  • renderLargeRectangles($rectangles);  
    1. abstract class Shape{     
    2.     protected $width = 0;     
    3.     protected $height = 0;     
    4.     abstract public function getArea();     
    5.     public function render($area)    {        // ... 
    6.     } 
    7. class Rectangle extends Shape{     
    8.     public function setWidth($width) 
    9.     {        $this->width = $width; 
    10.     }     
    11.     public function setHeight($height) 
    12.     {        $this->height = $height; 
    13.     }     
    14.     public function getArea() 
    15.     {        return $this->width * $this->height; 
    16.     } 
    17. class Square extends Shape{     
    18.     private $length = 0;     
    19.     public function setLength($length) 
    20.     {        $this->length = $length; 
    21.     }     
    22.     public function getArea() 
    23.     {        return pow($this->length, 2); 
    24.     } 
    25. function renderLargeRectangles($rectangles){     
    26. foreach ($rectangles 

        推荐阅读

        微服务架构中模块划分和服务识别

      沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散! 比来在进行微办事架构的交换和>>>详细阅读


      本文标题:PHP代码简洁之道——SOLID原则

      地址:http://www.17bianji.com/lsqh/38154.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)