How to write a Class A comedy a network name one over

Source: Internet
Author: User
Data-id= "1190000004968048" data-licence= "" >

"Code Daquan" a book about the concept of abstract data Model (ADT), in the view of PHP, the usual most common class is the expression of the abstract data model, many people on object-oriented programming is still in the mengmengdongdong stage, often what methods are stuffed in a class, for example, we have a font class, the code is as follows:


  
   

这个类中定义了公有的属性"style",获取Style的方法和另外一些子方法,本来创建这个类是为了围绕「字体」作文章的,但在实现过程中,临时需求的增加使得这个类中多了Div和把字体插入到Div的方法,这是很常见的情况,而这只是个开始,与类无关的代码会越来越多,就像地上本无垃圾,有人丢了第一块垃圾,人们就会跟着丢垃圾一样,最后的结果就是这个类变得不易维护,可读性减低,导致这样的原因就是与类无关的代码破坏了这个类抽象的一致性。

另外这个类的style属性也有问题,它的类型是public,意味着外部可以随时调用改写,然后外部代码就有了

$font = new Font;$font->style = 'font-size: 13px';

或者:

$font = new Font;$font->style = 'font-weight: both';

等等等等,日积月累,某一天,产品走到你面前,微微一笑地对你说:陈工啊,麻烦给每个字体都加上个默认的14px字体大小样式;

你一脸懵逼带着生无可恋的表情望着产品(他或者她或者它):……好,请你给我半天或更长的时间来改;

产品:#¥%……&*(&……%#¥%……&*;

你一边查找替换着代码,一边暗骂自己为什么不在Font类中定义个函数来修改style属性,一边还总结着经验告诉自己下次绝不这么傻逼了。程序员总是在一次次的惨痛经验中进步着,但如果有那么一本好书籍或者好导师,真的会少走很多弯路。(再给代码大全打个广告)

第一段我们讲到抽象的概念,抽象是什么呢?面向对象中有个原则既单一原则,我理解的抽象跟这个单一原则有着千丝万缕的关系;单一原则分为类的单一和方法的单一,比如说,人有吃喝拉撒四种行为,他们即有类的单一原则(人),也有方法的单一原则(吃、喝、拉、撒),这里的「人」就是我们抽象出来的东西,如果你往里面加个飞行的行为,就破坏了类的单一原则和抽象性,来验证下,加入飞行行为之后人就有了「吃、喝、拉、撒、飞行」五个行为,这不是人应该是鸟了。

「高内聚、低耦合」是写好类的核心思想,抽象(单一性)能帮助我们写出低耦合的代码,那高内聚呢,内聚性会直接表现在我们的代码维护上,当我们写出内聚性很强的代码时,与之关联的维护工作也会变得简单。上面的栗子讲到这个程序员把style属性设为public并且在外部代码中直接修改导致了后面的问题产生;对这些易改变的元素我们应该通过子程序(方法)修改的方式来增强它们的维护性,把所有的变化都控制在类中来增强类的内聚性。

一切理论都是虚的,实践才是让自己进步的最佳方法,Talk is cheap. Show me the code。

以上就介绍了怎样写好一个类,包括了一个,怎样方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.