码迷,mamicode.com
首页 > 编程语言 > 详细

【Effective Java】阅读

时间:2017-10-07 14:36:48      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:increase   枚举类   选择算法   fun   访问   atom   equals   nat   创建   

Java写了很多年,很惭愧,直到最近才读了这本经典之作《Effective Java》,按自己的理解总结下,有些可能还不够深刻

一、Creating and Destroying Objects
  1. Consider static factory methods instead of constructors (factory方法可以拥有名称,可以避免重复创建,比如单例模式)
  2. Consider a builder when faced with many constructor parameters(对于多个参数的比较适用,更加灵活,比如有两个相同类型参数的构造函数无法分辨的情况)
  3. Enforce the singleton property with a private constructor or an enum type(单例模式,1.5后用枚举实现更合适)
  4. Enforce noninstantiability with a private constructor (不允许实例化的对象写一个私有构造函数,以避免滥用,比如工具类)
  5. Avoid creating unnecessary objects (不要多余的创建类,比如在循环中去反复创建对象,特别需要避免的是基本类型和装箱类型的反复转换)
  6. Eliminate obsolete object references (比如对于pop操作,pop一个元素后要把数组的那一位无用的元素置为null)
  7. Avoid finalizers(除了一些关闭资源操作,不要再finally里包含不必要的处理)
二、Methods common to all objects
  1. Obey the general contract when overriding equals (equals方法覆盖时参数等必须对照上)
  2. Always override hashcode when override equals (equals覆盖时hashcode方法也要override,因为在集合类中的操作时为了快速会先比较hashcode)
  3. Always override toString (主要是为了使外界更容易了解实例信息)
  4. Override clone judiciously (clone方法要保证不改变原有实例)
  5. Consider implementing Comparable (对于需要排序的情况,实现Comparable接口是个好的选择)
 
三、Classes and interfaces
  1. Minimize the accessibility of classes and members (只给类和方法最小的访问权限是防止滥用的第一步)
  2. In public classes, user accessor methods, not public fields (这也是POJO的原因,因为可以在get,set方法里做额外的事情并且对于变化可控)
  3. Minimize mutability (尽可能限制可变,对于不需要变更要限制,加上final,private等限制)
  4. Favor composition over inheritance (组合优先于继承,其实继承是对封闭原则的一种破坏)
  5. Design and document for inheritance or else prohibit it (对于父类,需要写明注释)
  6. Prefer interfaces to abstract classes (接口优先于抽象类,因为继承只能是一个来源)
  7. Use interfaces only to define types (可用接口来声明类别)
  8. Preffer class hierachies to tagged classes (所谓的标签类是指一个类中拥有标签的属性,比如类可以被tag成圆形或者方形,这种情况用继承比较好)
  9. Use function objects to represent strategies (strategy pattern是策略模式,是允许在运行时选择算法的一种模式)
  10. Favor static member classes over nonstatic (对于不需要外界访问的内部类,用static是为了减少开销)
四、Generics
  1. Don‘t use raw types in new code (对于list,set等最好不要用raw类型,指明类型有助于提前让编译器发现错误)
  2. Eliminate unchecked warnings (也是为集合类而言)
  3. Prefer lists to arrays (推荐用list的原因也在于编译器可以发现错误,而数组不能检查元素类型)
  4. Favor generic types (E,V这种类型有助于代码复用)
  5. Use bounded wildcards to increase API flexibility (通配符?类型有助于API的灵活性)
  6. Consider typesafe heterogeneous containers 
 
五、Enums and annotations
  1. Use enum instead of int constants (这个不用多说)
  2. Use instance fields instead of ordinals (对于枚举类,不要用ordinals来取其整数值)
  3. Use EnumSet instad of bit fields (用枚举集合来代替“位”属性,因为位移这种属性实在可读性差)
  4. Use EnumMap instead of ordinal indexing (用枚举的map代替ordinal方法来排序或者索引)
  5. Emulate extensible enums with interfaces (用接口实现枚举的可扩展性)
  6. Prefer annotations to naming patterns (注解的方法可以避免拼写等错误)
  7. Consistently use the Override annotation (如果是覆盖,用override注解能让编译器来检查错误)
  8. Use marker interfaces to define types (接口定义类型)
六、Methods
  1. Check parameters for validity (方法内检查参数的有效性是必要的)
  2. Make defensive copies then needed (所谓防卫性的copy,是为了防止外界构造实例后再次更改内部数据)
  3. Design method signatures carefully (方法的命名https://en.wikipedia.org/wiki/Naming_convention_(programming),参数个数不要过多)
  4. Use overloading judiciously (重载方法的选择是静态的,而重写方法的选择是动态的)
  5. Use varargs judiciously (可变参数的缺点运行时才会发现,可以用传递第一个必要的参数,剩下的用可变参数来解决)
  6. Return empty arrays or collections not nulls (减少空指针错误)
  7. Write doc comments for all exposed API elements (注释)
 
七、General programming
  1. Minimize the scope of local variables (封闭性原则)
  2. Prefer for-each loops to traditional for loops (减少中间参数使用)
  3. Know and use the libraries (尽量使用标准库的方法)
  4. Avoid float and double if exact answers are required (flocat,double是科学计算而生,如果需要精确值,使用int,long,BigDecimal代替)
  5. Prefer primitive types to boxed primitives (尽量使用基本类型)
  6. Avoid strings where other types are more approprivate (String虽然使用简单,但能用精确类型更好)
  7. Beware the performance of string concatenation (老生常谈问题,字符拼接用StringBuilder)
  8. Refer to obejects by their interfaces (用接口来做类型的引用会使代码更加灵活)
  9. Prefer interfaces to reflection (反射虽然好用但很多问题只有在运行时才能被发现)
  10. Use native methods judictiously (JVM为了支持多个语言允许native方法但一般不这样做,如果仅仅是为了提高性能)
  11. Optimize judiciously (改善性能的时候多考虑更改带来的后果)
  12. Adhere to generally accepted naming conventions (Java命名规范:http://docs.oracle.com/javase/specs/jls/se7/html/index.html
 
八、Exceptions
  1. Use exception only for exceptional conditions (正常逻辑不应该用Exception)
  2. Use checked exceptions for recoverable conditons and runtime programming errors (需要继续操作的用checked exception)
  3. Avoid unnecessary use of check exceptions
  4. Favor the use of standard exceptions
  5. Throw exceptions appropriate to the abstraction
  6. Document all exceptions thrown by each method
  7. Include failure-capture information in detail message
  8. Strive for failure atomicity (异常的原子性)
  9. Don‘t ignore exceptions
 
九、Concurrency
  1. Synchronize  access to shared mutable data
  2. Avoid excessive synchronization (同步不要泛滥)
  3. Prefer executors and tasks to threads
  4. Prefer concurrency utilities to wait and notify
  5. Document thread safety
  6. Use lazy initialization judiciously
  7. Don‘t depend on the thread scheduler
  8. Avoid thread groups
 
十、Serialization
  1. Implement serializable judiciously
  2. Consider using a custom serialized form
  3. Write readObject method defensively 
  4. For instance control, prefer enum types to readResolve
  5. Consider serializations proxies instead of serialized instances
 
 
 

 

【Effective Java】阅读

标签:increase   枚举类   选择算法   fun   访问   atom   equals   nat   创建   

原文地址:http://www.cnblogs.com/roostinghawk/p/7631809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!