Diposkan pada Technology

Effective Java Summary

Hi there, since long time no post and update here, i decided to post my notes aobut book that i currently learn, the book is effective java, xD. Yeah it’s so geek but i think it will be make use if i post the summary so someone else who read it can got the point
here there are:

chapter 1. Creating and Destroying Objects

  1. Consider static factory methods instead of constructors

    1. Has name, easy to understand what it does
      Names describe lot
    2. Not required to create a new object each time invoked
      Can access class feature without instantiate it
    3. They can return an object of any subtype of their return type
      They can access class field without instantiate
    4. they reduce the verbosity of creating parameterized type instances
      It could call and assign parameter to constructor without use knownwhat it’s parameterized

      • classes without public or protected constructors cannot be subclassed. Karena pake static factory method biasanya or harusnya constructornya private, jadinya ga bisa di subclass
      • they are not readily distinguishable from other static methods.
      • Here are some common names for static factory methods:
        1. valueOf = return value of parameter
        2. of = alternative of valueOf.
        3. getInstance = return instance, in case of singleton it takes no params and returns the sole instance.
        4. newInstance = guarantees that each instance returned is distinct from all other.
        5. getType = like get instance.
        6. newType = like new instance.
        7. In summary, static factory methods and public constructors both have their uses, and it pays to understand their relative merits. Often static factories are preferable, so avoid the reflex to provide public constructors without first considering static factories.
  2. Consider a builder when faced with many constructor parameters

    1. the telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to read it.
    2. a JavaBean may be in an inconsistent state partway through its construction
    3. The JavaBeans pattern precludes the possibility of making a class immutable
    4. A minor advantage of builders over constructors is that builders can have multiple varargs parameters.
    5. The Builder pattern is flexible.
    6. The builder can fill in some fields automatically
      1. The Builder pattern does have disadvantages of its own. In order to create an object, you must first create its builder.
      2. Also, the Builder pattern is more verbose than the telescoping constructor pattern, so it should be used only if there are enough parameters, say, four or more. But keep in mind that you may want to add parameters in the future.
        The Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters, especially if most of those parameters are optional.
  3. Enforce the singleton property with a private constructor or an enum type

    1. Keeping the constructor private and exporting a public static member to provide access to the sole instance. The private constructor is called only once, to initialize the public static final field
      • the declarations make it clear that the class is a singleton
    2. The public member is a static factory method: Using public static getInstance() Methods which returns sole object.
      • it gives you the flexibility to change your mind about whether the class should be a singleton without changing its API
    3. Third approach to implementing singletons. Simply make an enum type with one element:
      This approach is functionally equivalent to the public field approach, except that it

      1. is more concise,
      2. provides the serialization machinery for free,
      3. and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks.
      4. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.
        1. Enforce noninstantiability with a private constructor
        2. a class can be made noninstantiable by including a private constructor.
  4. Avoid creating unnecessary objects

    1. Object is costly use it wisely, sometimes object only need to initialized once use static {} if an object just need to initialized once
    2. prefer primitives to boxed primitives, and watch out for unintentional autoboxing.
    3. object pool is a bad idea unless the objects in the pool are extremely heavyweight.
  5. Eliminate obsolete object references

  6. Memory leak slow down programs.

    unintentional object retentions

    1. Manually nulling out array elements as soon as they become part of the inactive portion.
    2. Another common source of memory leaks is caches
    3. A third common source of memory leaks is listeners and other callbacks.
  7. Avoid finalizers

    1. Finalizers are unpredictable, often dangerous, and generally unnecessary.
    2. Never do anything time-critical in a finalizer.
    3. Never depend on a finalizer to update critical persistent state.
    4. There is a severe performance penalty for using finalizers.

Satu tanggapan untuk “Effective Java Summary

  1. factory and builder, although useful sometimes (mainly when we consider the case where it is easier to do Object mocking in TDD/unit test), is too over-engineered most of the times xD xD xD

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s