Professional Documents
Culture Documents
b. Unlike constructors, they are not required to create new object each time they are
invoked.
c.
They can return any subtype of their return type. This technique leads to interface
based frameworks.
f.
Static factory methods are not readily distinguishable from other static methods
unless we follow proper naming conventions like(valueOf, of, getInstance,
newInstanace, getType, newType etc)
Service Access API: Which clients use to obtain an instance of the service.
Instead of making the desired object directly, the client calls the constructor or
static factory with required parameters and gets a builder object.
b. Then the client calls setters like methods to set the optional parameters based on
the interest.
c.
Finally client calls the parameter less build() method to generate the object.
Example:
NutritionFacts.java
3.
c.
There are two approaches public attribute with private constructor and public
factory method with private constructor.
SingleTon.java
f.
The above enum approach is equivalent to public field approach. The additional benefit
is, it provides the serialization machinery for free.
Of all the approaches single element enum is the best way to implement a singleton.
4.
AbstractClass and/or Final doesnt fully prevents us from instantiating the class.
Reuse can be faster and stylish. It is definitely applicable in case the object is
immutable. Ex. String str = new String(DO NOT DO THIS);
b. Consider using static factory methods instead of constructors. For example prefer
using Boolean.valueOf() to new Boolean();
c.
It is also valid for mutable objects where we are sure that created objects will
never be modified.
6.
Nulling the object references should be the exception rather than the norm.
b. The best way to eliminate an obsolete reference is to let the variable that contained
the reference fall out of scope. This occurs naturally if you define each variable in
the narrowest possible scope
c.
Generally speaking, whenever a class manages its own memory, the pro-grammer
should be alert for memory leaks.
7.
Avoid Finalizers.
a.
2. using raw types can lead to exceptions at runtime, so dont use them in new code.