【设计模式:单例模式】使用单例模式载入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();
}
}
}
}
因为初始化放在内部类中,仅仅有当此内部类被使用时,才会进行初始化。
从而既节省了空间。也无需同步代码。
【设计模式:单例模式】使用单例模式载入properties文件的更多相关文章
- Java-马士兵设计模式学习笔记-观察者模式-读取properties文件改成单例模式
一.概述 1.目标:读取properties文件改成单例模式 二.代码 1.Test.java class WakenUpEvent{ private long time; private Strin ...
- spring 配置文件中使用properties文件 配置
配置Bean载入properties文件: <bean id="propertyPlaceholderConfigurer" class="org.springfr ...
- 【设计模式:单例模式】使用单例模式加载properties文件
先准备测试程序: package org.jediael.util; import static org.junit.Assert.*; import org.junit.Test; public c ...
- 【白话设计模式四】单例模式(Singleton)
转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...
- iOS设计模式反思之单例模式的进化
什么是单例模式? 单例模式想一个大独裁者,他规定在他的国度里面,所有数据的访问和请求都得经过他,甚至你要调用相关的函数也得经过它.学术一点就是,单例模式,为某一类 需求和数据提供了统一的程序接口.主要 ...
- 【C++深入浅出】设计模式学习之单例模式
但凡成为大家公认的模式,都是有一些不可小觑的威力,今天分享一个简单的设计模式:单例模式. 单例模式用于一些只希望有一个实例的类或者只希望执行一次的操作:校长只能有一个.老板只能有一个.用户点击弹窗只希 ...
- php设计模式笔记:单例模式
php设计模式笔记:单例模式 意图: 保证一个类仅有一个实例,并且提供一个全局访问点 单例模式有三个特点: 1.一个类只有一个实例2.它必须自行创建这个实例3.必须自行向整个系统提供这个实例 主要实现 ...
- Java设计模式之《单例模式》及应用场景
摘要: 原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6510196.html 所谓单例,指的就是单实例,有且仅有一个类实例,这个单例不应该 ...
- Java设计模式之【单例模式】
Java设计模式之[单例模式] 何为单例 在应用的生存周期中,一个类的实例有且仅有一个 当在一些业务中需要规定某个类的实例有且仅有一个时,就可以用单例模式 比如spring容器默认初始化的实例就是单例 ...
随机推荐
- java多态的理解----部分非原创
所谓多态,其实就是对于同一件事情,不同的对象要采取不同的行为,或者同一个对象在不同的情况下需要采取不同的行为方式. 不同的对象要采取不同的行为: 这有两种实现方式:接口实现和子类重新父类方法.这两种实 ...
- Docker网络管理-外部访问容器
注意:这里使用的方法是端口映射,需要说明的是端口映射是在容器启动的时候才能完成端口映射的. 1,搭建1个web服务器,让外部机器访问. docker run -itd centos /bin/bash ...
- HTML - Textarea - 空格的问题解决方式
第一种方式: <textarea name="textareaname" rows="XX" cols="XX" ></t ...
- js获取url参数的方法
js获取url参数的方法有很多. 1.正则分析 function getQueryString(name) { var reg = new RegExp("(^|&)" + ...
- CString常用操作
①.CString 类对象的初始化: CString str; CString str1(_T("abc")); CString str2 = _T("defg" ...
- (转) launch failed.Binary not found in Linux/Ubuntu解决方案
原地址: http://blog.csdn.net/abcjennifer/article/details/7573916 Linux下出现launch failed.Binary not found ...
- Linux命令查询手册--sort
一.sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. 二.sort的参数选项 1. sort -u 在输出 ...
- alloc & init & dealloc
在Objective-C中,alloc,init和dealloc是经常使用的函数,那么它们内部到底是如何实现的呢?通过查看libobjc运行时库,可以发现他们的工作原理. 1 alloc alloc的 ...
- sublime快捷键收藏
快速查找(ctrl + P)输入@+函数名可以快速找到函数.输入#+文本可以快速进行文件内文本匹配.3. 多行游标功能(ctrl + D,非常实用)如何将文件中的某个单词更改为另一个?方法一:利用查找 ...
- 无聊写了一个最简单的MVC4+Highcharts连数据库例子
乱搞了个数据库 后面发现没定INT类型 直接将ID当数据显示了 效果图: 前端 @{ Layout = null; } <!DOCTYPE html> <html> <h ...