Java學習筆記(一)の考慮用靜態工廠方法代替構造器 – JAVA編程語言程序開發技術文章

靜態工廠方法與構造器不同的第一大優勢在於,它們有名稱。

靜態工廠方法與構造器不同的第二大優勢在於,不必在每次調用它們的時候都創建一個新對象。

靜態工廠方法與構造器不同的第三大優勢在於,它們可以返回原返回類型的任何子類型的對象。

靜態工廠方法的第四大優勢在於,在創建參數化類型實例的時候,它們使代碼變得更加簡潔。

 

 

靜態工廠方法的主要缺點在於,類如果不含有公有的或受保護的構造器,就不能被子類化。

靜態工廠方法的第二個缺點在於,它們與去他的靜態方法實際上沒有任何區別。

 

服務提供者框架:

[java] // Service provider framework sketch – Service interface   
 
public interface Service { 
    // Service-specific methods go here  

// Service provider framework sketch – Service interface

public interface Service {
    // Service-specific methods go here
}
[java] // Service provider framework sketch – Service provider interface   
 
public interface Provider { 
    Service newService(); 

// Service provider framework sketch – Service provider interface

public interface Provider {
    Service newService();
}[java] // Service provider framework sketch  
 
// Noninstantiable class for service registration and access   
 
import java.util.*; 
import java.util.concurrent.*; 
 
public class Services { 
    private Services() { }  // Prevents instantiation (Item 4)  
 
    // Maps service names to services  
    private static final Map<String, Provider> providers = 
        new ConcurrentHashMap<String, Provider>(); 
    public static final String DEFAULT_PROVIDER_NAME = "<def>"; 
 
    // Provider registration API  
    public static void registerDefaultProvider(Provider p) { 
        registerProvider(DEFAULT_PROVIDER_NAME, p); 
    } 
    public static void registerProvider(String name, Provider p){ 
        providers.put(name, p); 
    } 
 
    // Service access API  
    public static Service newInstance() { 
        return newInstance(DEFAULT_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(); 
    } 

// Service provider framework sketch

// Noninstantiable class for service registration and access

import java.util.*;
import java.util.concurrent.*;

public class Services {
    private Services() { }  // Prevents instantiation (Item 4)

    // Maps service names to services
    private static final Map<String, Provider> providers =
        new ConcurrentHashMap<String, Provider>();
    public static final String DEFAULT_PROVIDER_NAME = "<def>";

    // Provider registration API
    public static void registerDefaultProvider(Provider p) {
        registerProvider(DEFAULT_PROVIDER_NAME, p);
    }
    public static void registerProvider(String name, Provider p){
        providers.put(name, p);
    }

    // Service access API
    public static Service newInstance() {
        return newInstance(DEFAULT_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();
    }
}
[java] // Simple test program for service provider framework  
 
public class Test { 
    public static void main(String[] args) { 
        // Providers would execute these lines  
        Services.registerDefaultProvider(DEFAULT_PROVIDER); 
        Services.registerProvider("comp",  COMP_PROVIDER); 
        Services.registerProvider("armed", ARMED_PROVIDER); 
 
        // Clients would execute these lines  
        Service s1 = Services.newInstance(); 
        Service s2 = Services.newInstance("comp"); 
        Service s3 = Services.newInstance("armed"); 
        System.out.printf("%s, %s, %s%n", s1, s2, s3); 
    } 
 
    private static Provider DEFAULT_PROVIDER = new Provider() { 
        public Service newService() { 
            return new Service() { 
                @Override public String toString() { 
                    return "Default service"; 
                } 
            }; 
        } 
    }; 
 
    private static Provider COMP_PROVIDER = new Provider() { 
        public Service newService() { 
            return new Service() { 
                @Override public String toString() { 
                    return "Complementary service"; 
                } 
            }; 
        } 
    }; 
 
    private static Provider ARMED_PROVIDER = new Provider() { 
        public Service newService() { 
            return new Service() { 
                @Override public String toString() { 
                    return "Armed service"; 
                } 
            }; 
        } 
    }; 

 

摘自  horsttnann的專欄 

發佈留言