Você está na página 1de 4

1.

Consider StaticFactory methods instead of constructors


a. Unlike constructors, we can provide meaningful names invoked. c. They can return any subtype of their return type. This technique leads to interface based frameworks. d. Reduces verbosity of creating parameterized type instances. e. f. Only static factory methods having no public/protected constructors cant be sub classed. 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 Provider framework: There are three essential components in service provider framework. 1. Service Interface: which providers implement clients access to them. 3. Service Access API: Which clients use to obtain an instance of the service. of their service implementation. Example: JDBC a. c. Service Interface : Connection Service Access API: DriverManager. getConnection() 4. Service provider interface(optional): Which providers implement to create instance 2. Provider Registration API: Which system uses to register implementations giving b. Unlike constructors, they are not required to create new object each time they are

b. Provider Registration API: DriverManager.registerDrivers() d. Service Provider Interface: Driver 2.

Consider Builder when faced with many constructor parameters.


a. 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.

Enforce the singleton property with a private constructor or an enum type.


a. Singleton is a class that is instantiated exactly once. b. Making singleton can make it difficult to test its clients.

c.

There are two approaches public attribute with private constructor and public factory method with private constructor.

d. The advantage of factorymethod approach is implementation details can be hidden. e. In case of a singleton class that is implementing serializable interface following additional things to be taken care. i. All the attributes should be made transient ii. Implement readResolve method and return the same instance [in the below example return INSTANCE

SingleTon.java

f.

From Java 1.5 onwards, we can use enums. i. Example: public enum SingleTon{ INSTANCE; Public getValue(){ //provide implementations. } }

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.

Enforce noninstantiability with a private constructor.


a. Utility classes were not designed to be instantiated. Throwing an error ensures that the constructor is not being invoked from within the class. c. AbstractClass and/or Final doesnt fully prevents us from instantiating the class. b. These classes need to have private constructors that throw AssertionError.

5.

Avoid creating unnecessary objects.


a. 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. d. Heavy use of autoboxing causes performance issues

6.

Eliminate Obsolete Object references.


a. Nulling the object references should be the exception rather than the norm. 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. d. Another common source of memory leaks is caches. e. A third common source of memory leaks is listeners and other callbacks. b. The best way to eliminate an obsolete reference is to let the variable that contained

7.

Avoid Finalizers.
a. Finalizers are unpredictable, often dangerous, and generally un-necessary b. There is a severe performance penalty for using finalizers.

Generics And Collections


1. We shouldnt use raw types such as List. 2. using raw types can lead to exceptions at runtime, so dont use them in new code.

Você também pode gostar