zoukankan      html  css  js  c++  java
  • 【设计模式:单例模式】使用单例模式加载properties文件


    先准备测试程序:

    package org.jediael.util;
    import static org.junit.Assert.*;
    import org.junit.Test;
    
    public class BasicConfigurationTest {
    	@Test
    	public void testGetValue(){
    		BasicConfiguration configuration = BasicConfiguration.getInstance();
    		assertTrue(configuration.getValue("key").equals("value"));
    	}
    }
    

    其中properties文件中有一行如下:

    key=value


    优先选择方案三


    方式一:懒汉方式

    到第一次使用实例时,才加载实例

    package org.jediael.util;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class BasicConfiguration {
    
    	private static BasicConfiguration configuration = null;
    	private Properties pros = null;
    	
    	public static synchronized BasicConfiguration getInstance(){
    		if(configuration == null){
    			configuration = new BasicConfiguration();
    		}
    		return configuration;
    	}
    	
    	public String getValue(String key){
    		return pros.getProperty(key);
    	}
    	
    	private BasicConfiguration(){
    		readConfig();
    	}
    
    	private void readConfig() {
    		pros = new Properties();		
    		InputStream in = null;
    		try {
    			in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("")
    					.getPath() + "search.properties");
    			pros.load(in);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}catch (IOException e) {
    			e.printStackTrace();
    		}finally{
    			try {
    				in.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    
    上述程序中,产生了BasicConfiguration的一个单例。

    好处是只有到第一次调用getInstance才生成对象,节省了空间。不足之处在于同步锁导致有可能执行过慢。

    2、饿汉方式

    package org.jediael.util;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class BasicConfiguration {
    
    	private static BasicConfiguration configuration = new BasicConfiguration();
    	private Properties pros = null;
    	
    	public static BasicConfiguration getInstance(){
    		return configuration;
    	}
    	
    	public String getValue(String key){
    		return pros.getProperty(key);
    	}
    	
    	private BasicConfiguration(){
    		readConfig();
    	}
    
    	private void readConfig() {
    		pros = new Properties();		
    		InputStream in = null;
    		try {
    			in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("")
    					.getPath() + "search.properties");
    			pros.load(in);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}catch (IOException e) {
    			e.printStackTrace();
    		}finally{
    			try {
    				in.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    

    由于BasicConfiguration的实例是static,因此,当类被加载时就会初始化,但这样即使并不需要使用此实例,也会被初始化,导致内存空间的浪费。


    方式三:

    package org.jediael.util;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class BasicConfiguration {
    
    	
    	private Properties pros = null;
    	
    	private static class ConfigurationHolder{
    		private static BasicConfiguration configuration = new BasicConfiguration();
    	}
    	
    	public static BasicConfiguration getInstance(){
    		return ConfigurationHolder.configuration;
    	}
    	
    	public String getValue(String key){
    		return pros.getProperty(key);
    	}
    	
    	private BasicConfiguration(){
    		readConfig();
    	}
    	
    
    
    	private void readConfig() {
    		pros = new Properties();		
    		InputStream in = null;
    		try {
    			in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("")
    					.getPath() + "search.properties");
    			pros.load(in);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}catch (IOException e) {
    			e.printStackTrace();
    		}finally{
    			try {
    				in.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }

    由于初始化放在内部类中,只有当此内部类被使用时,才会进行初始化。从而既节省了空间,也无需同步代码。





  • 相关阅读:
    Windows Store App 主题动画
    Windows Store App 过渡动画
    Windows Store App 控件动画
    Windows Store App 近期访问列表
    Windows Store App 文件选取器
    Windows Store App 访问应用内部文件
    Windows Store App 用户库文件分组
    Windows Store App 获取文件及文件夹列表
    Windows Store App 用户库文件夹操作
    Windows Store App 用户库文件操作
  • 原文地址:https://www.cnblogs.com/eaglegeek/p/4557898.html
Copyright © 2011-2022 走看看