修改《java与模式》中有一个关于多例模式的一个多语言支持的一个例子的错误

java与模式中有一个关于多例模式的一个多语言支持的一个例子,但程序有点问题,我把它修改了一下。
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;


public class LingualResource {
	/**
	 * This is the language attribute
	 */
	private String language = "en";
	
	/**
	 * This is the country attribute
	 */
	private String region = "US";
	
	/**
	 * This is the localeCode attribute
	 */
	private String localeCode = "en_US";
	
	/**
	 * This is the file name of the resource bundle
	 */
	private final static String FILE_NAME = "res";
	
	/**
	 * This collection keep many LingualResource object according to there locale code.
	 */
	private static Map<String,LingualResource> instances = new HashMap<String,LingualResource>(19);
	
	/**
	 * This is the locale object, is initialized by language and region
	 */
	private Locale locale = null;
	
	/**
	 * This is the ResourceBundle object, initialized by the locale and the 
	 * base file name.
	 */
	private ResourceBundle resourceBundle = null;
		
	/**
	 * This is the default construct
	 */
	private LingualResource(){}
	
	/**
	 * This is another overloaded constructor
	 * @param language The language attribute
	 * @param region The region attribute
	 */
	private LingualResource(String language,String region){
		this.language = language;
		this.region = region;
		this.localeCode = makeLocaleCode(language, region);
		this.locale = new Locale(language,region);
		resourceBundle =  ResourceBundle.getBundle(FILE_NAME,this.locale);
		instances.put(this.localeCode, this);
	};
	
	/**
	 * This is a factory method to return a LingualResource.
	 * It first judge if it is has existed in the Map collection,
	 * if it currently has existed in the map, then directly return it.
	 * Otherwise directly new a object. Pay attention to the constructor,
	 * in the constructor code body, the newly constructed object will
	 * be put into the collection 
	 * @param language The language attribute.
	 * @param region The region attribute.
	 * @return
	 */
	public static synchronized LingualResource getInstance(String language,String region){
		System.out.println(instances);
		if(instances.containsKey(makeLocaleCode(language,region))){
			System.out.println(instances.get(makeLocaleCode(language,region)).getClass());
			return (LingualResource)instances.get(makeLocaleCode(language,region));
		} else {
			return new LingualResource(language,region);
		}
		
	}
	
	/**
	 * This method is to make up the locale code according to the language attribute and the
	 * region attribute
	 * @param language The language attribute
	 * @param region The region attribute.
	 * @return
	 */
	private static String makeLocaleCode(String language,String region){
		return language + "_" + region;
	}
	
	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public ResourceBundle getResourceBundle() {
		return resourceBundle;
	}

	public void setResourceBundle(ResourceBundle resourceBundle) {
		this.resourceBundle = resourceBundle;
	}

	public String getLocaleString(String code){
		return resourceBundle.getString(code);
	}
}



客户端调用代码:

public class LingualResourceTester {
	public static void main(String[] args)
    {
        LingualResource ling = 
            LingualResource.getInstance("en" , "US");
        String usDollar = ling.getLocaleString("USD");
        System.out.println("USD=" + usDollar);
        LingualResource lingZh = 
            LingualResource.getInstance("zh" , "CH");
        String usDollarZh = lingZh.getLocaleString("USD");
        System.out.println("USD=" + usDollarZh);
        
        LingualResource lingTwo = 
            LingualResource.getInstance("en" , "US");
        String usDollarTwo = lingTwo.getLocaleString("USD");
        System.out.println("USD=" + usDollarTwo);
        LingualResource lingZhTwo = 
            LingualResource.getInstance("zh" , "CH");
        String usDollarZhTwo = lingZhTwo.getLocaleString("USD");
        System.out.println("USD=" + usDollarZhTwo);
    }
}



本地化文件:
// res_en_US.properties
USD=US Dollar
JPY=Japanese Yen


// res_zh_CH.properties
USD=美元
JPY=日元

你可能感兴趣的:(java,UP)