单例模式(SinglePattern)的应用

       单例模式经常在使用在配置文件的读取上,这样整个工程只生成一个读取配置文件的实例.它经常和简单工厂模式一起使用.所以,偶经常把这两个模式整合起来叫做静态工厂模式.也不知道对不对,欢迎交流哦~

       一般文献上说单例模式有三种类型,分别是:饿汉式,懒汉式和登记式.感觉自己经常接触的就是第一种饿汉式和懒汉式的,他们很相似,因为相对来说比较简单.

以前模仿后写的东西,有一年了吧,现在拿出来晒晒.是属于第二种的.

/**
 *读取属性类.因为是用做配置类型信息的
 *单例模式只创建一个实例
 
*/

public   class  SProperties  {

    
private static final String PFILE ="database.properties";
    
private File file = null;

    
private long lastModifiedTime = 0//判断最后的修改时间以便读取最新的设置信息..

    
private Properties props = null;

    
private static SProperties instance = null;

    
//构造函数私有化.....
    private SProperties() {
        file 
= new File(PFILE);
        lastModifiedTime 
= file.lastModified();
        
if (lastModifiedTime == 0{
            System.err.println(PFILE 
+ "file does not exist");
        }


        props 
= new Properties();
        
try {

            props.load(
new FileInputStream(PFILE));

        }
 catch (IOException e) {
            e.printStackTrace();
        }

    }


    
/**
     *
@return  SinglePattern的单一实例
     
*/

    
synchronized public static SProperties getInstance()

    
{  if(instance==null)
                  new   SProperties() ;
           
return instance;
    }


    
//读取一个属性项
    final public Object getConfigItem(String name, Object defaultVal) {

        
long newTime = file.lastModified();
        
if (newTime == 0{
            
if (lastModifiedTime == 0{

                System.err.println(PFILE 
+ "file does not exist");
            }
 else {
                System.err.println(PFILE 
+ "file was  deleted");
            }


        }
 else if (newTime > lastModifiedTime) {
            props.clear();
            
try {
                props.load(
new FileInputStream(PFILE));
            }
 catch (IOException e) {
                e.printStackTrace();
            }

        }


        lastModifiedTime 
= newTime;
        Object val 
= props.getProperty(name);
        
if (val == null{
            
return defaultVal;
        }
 else {
            
return val;
        }


    }

}

 

调用:

   // 获得连接
       public     static   Connection  getConnection()
      
{
             String drivers 
=SProperties.getInstance().getConfigItem("jdbc.drivers","No found").toString();
             
//System.out.println (drivers);
                String  url  =SProperties.getInstance().getConfigItem("jdbc.url","No found").toString();
                
//System.out.println (url);
           
                String  username
=SProperties.getInstance().getConfigItem("jdbc.username","No found").toString();
                String  password
=SProperties.getInstance().getConfigItem("jdbc.password","No found").toString();
                
                
//System.out.println (username);
                
//System.out.println (password);
                try
                
{
                   Class.forName(drivers);
                   connect
=DriverManager.getConnection(url,username,password);
                }
catch(ClassNotFoundException  no)
                

                      System.out.println(
"ClassNotFound  Exception:::");
                      
                }
catch(SQLException  sqle)
                
{
                      System.out.println(
"SQLException");
                }

                
                
return   connect;  
             
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值