Effective Java 英文 第二版 读书笔记 Item 1: Consider static factory methods instead of constructors

最近每天上班下班有点时间看下 Effective Java。

我一般看看原文,在看看示例代码,再想想原文的意思。

我英文也不是很好,所以决定中文英文随便用.

 

代码部分主要是用sublime敲的,可能存在错误。

 

Creating and destroying objects

Item 1: Consider static factory methods instead of constructors 

 

Advantage of static factory methods

1.Unlike constructors. They have names.

 

2.They are not required to create a new object each time they’re invoked.

 

3.They can return an object of any subtype of their return type.

 

4.They reduce the verbosity of creating parameterized type instances.

  

 

1.

public static Boolean valueOf(boolean b){
    return b?Boolean.TRUE:Boolean.FALSE;
}

 

3.

A service provider framework.

a service interface,which providers implement;

a provider registration API,which the system uses to register implementations,giving clients access to them;

a service access API,which clients use to obtain an instance of the service.

optional component a service provider interface,which providers implement to create instances of their service implementation.

 

In the case of JDBC . Connection plays the part of the service interface,DriverManager.registerDriver is the provider registration API,DriverManager.getConnection is the service access API,and Driver is the service provider interface.

 

//Service provider framework sketch

//Service interface
public interface Service{
    //Service-specific methods go here
}

//Service provider interface
public interface Provider{
    Service newService();
}

// Noinstantiable class for service registration and access 
public class Services{
    private Services(){} // Prevents instantiation 

    //Maps service names to services 
    private static final Map<String,Provider> providers = new ConcurrentHashMap<String,Provider>();
    
    public static final String DEFAUL_provider_name="<def>"//Provider registration API
    public static void registerDefaultProvider(Provider p){
        registerProvider(DEFAUL_provider_name,p);
    }
    public static void registerProvider(String name,Provider p){
        providers.put(name,p);
    }

    //Serivce access API
    public static Service newInstance(){
        return newInstance(DEFAUL_provider_name);
    }
    public static Service newInstance(String name){
        Provider p=providers.get(name);
        if (p==null {
            throw new IllegalArgumentException(
                "No provider registered with name: "+name);
            return p.newService();
            
        }

    }

}

 

4.

    //the general way to create a instance,clearly the right part of The assignment is repeated
    Map<String,List<String>> m=new HashMap<String,List<String>>();

    //suppose that HashMap providerd this static factory:
    public static <K,V> HashMap<K,V> newInstance(){
        return new HashMap<K,V>();
    }

    //Then you could replace the wordy declaration above with this succinct alternative:
    Map<String,List<String>> m=HashMap.newInstance();

 

 

Disadvantage of static factory methods

classes without public or protected constructors cannot be subclassed.

they are not readily distinguishable from other static methods.

 

 

 

 

 

 

ValueOf--Returns an instance that has,loosely speaking,the same value as its parameters.Such static factories are effectively type-conversion methods.

Of--A concise alternative to valueOf,popularized by EnumSet.

getInstance--Returns an instance that is described by the parameters but cannot be said to have the same value.In the case of a singleton,getInstance takes no parameters and returns the sole instance

newInstance--Like getInstance,except that newInstance guarantees that each instance returned is distinct from all others.

getType--Like getInstance,but used when the factory method is in a different class.Type indicates the type of object returned by the factory method.

newType--Like newInstance,but used when the factory method is in a different class.Type indicates the type of object returned by the factory method.

 

 

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.

 

posted @ 2015-08-20 16:55  郁闷紫番薯  阅读(288)  评论(0编辑  收藏  举报