【设计模式:单例模式】使用单例模式载入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容器默认初始化的实例就是单例 ...
随机推荐
- [RxJS] Reactive Programming - What is RxJS?
First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...
- iOS 8 Metal Swift教程(一) :开始学习
在本篇教程中,你将应用到3D图形中的一系列矩阵变换,并会学习到如下内容: 如何使用模型(model),视图(view)以及投影变换(projection transformations). 如何使用矩 ...
- Docker网络管理-外部访问容器
注意:这里使用的方法是端口映射,需要说明的是端口映射是在容器启动的时候才能完成端口映射的. 1,搭建1个web服务器,让外部机器访问. docker run -itd centos /bin/bash ...
- jwplayer去Logo、自定义公司信息、限制拖动
function initplayer(){ jwplayer("mediaplayer").setup({ primary: "fl ...
- 转:Dictionary<int,string>怎么获取它的值的集合?急!急!急!
怎么获取Dictionary<int, string>的值?我知道这个是键值对的,我知道可以根据key得到value,但关键是现在连key也不知道啊就是想让这个显示在listbox中,应该 ...
- I/O多路复用之epoll
1.select.poll的些许缺点 先回忆下select和poll的接口 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set ...
- boost vc编译
0.下载:http://www.boost.org/ 1.编译b2.exe,bjam.exe. 双击根目录下面的bootstrap.bat文件,生成b2.exe,bjam.exe(或者使用vs的命令行 ...
- CentOS安装配置ganglia
1. 下载ganglia源码包并解压 wget http://sourceforge.net/projects/ganglia/files/ganglia%20monitoring%20cor ...
- Android实现点击事件的4种方式
一.通过在activity_main.xml中,按钮button控件中添加onclick事件实现 在 activity_main.xml 对应的按钮Button中加入下面红色事件 <Butt ...
- c#或获取系统的特殊路径,如我的文档等
Console.WriteLine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); System.E ...