单例模式

来源:互联网 发布:淘宝标题品牌侵权 编辑:程序博客网 时间:2024/04/28 17:12

一、使用场景:
1.在程序运行过程中只需要创建该类的一个实例
2.创建该类的实例很费时间,比如数据库连接,日志读配置文件

二、实现:
饿汉式单例模式

package com.DesignPattern;public class Singleton {    private static Singleton instance = new Singleton();    private Singleton(){      //注意一定要将构造函数私有化        // ...    }    public static Singleton getInstance(){        return instance;    }}

懒汉式

package com.DesignPattern;public class Singleton {    private static Singleton instance = null;    private Singleton(){ //注意一定要将构造函数私有化        // ...    }    public static Singleton getInstance(){        if(instance==null){            instance = new Singleton();        }        return instance;    }}

三、增加同步

public static synchronized Singleton getInstance(){        if(instance==null){            instance = new Singleton();        }        return instance;    }

这要多线程取得时候只能一个一个取,效率低

public static  Singleton getInstance(){        if(instance==null){ //如果多个线程到达这里,则可能创建多个实例            synchronized(Singleton.class){                instance = new Singleton();            }        }        return instance;    }

这样可能创建多个实例

双检锁机制

public static  Singleton getInstance(){        if(instance==null){            synchronized(Singleton.class){                if(instance==null){                    instance = new Singleton();                }            }        }        return instance;    }

四、模拟日志系统

package com.DesignPattern;import java.io.FileWriter;import java.io.InputStream;import java.io.PrintWriter;import java.util.Date;import java.util.Properties;/* * 日志系统 */public class MyLogSystem {    private static PrintWriter out;    private static MyLogSystem logger;    private MyLogSystem(){    System.out.println("New Log!");        init();    }    public synchronized void log(String content){        if(logger==null||out==null){            logger = new MyLogSystem();        }        try{            out.println(new Date()+":"+content);        }catch(Exception e){            System.out.println("输出失败");            e.printStackTrace();        }    }    static MyLogSystem getLogger() {        if(logger==null){                         synchronized(MyLogSystem.class){                if(logger==null){                          //双检锁机制                    logger = new MyLogSystem();                }            }        }        return logger;    }    private void init(){        try{            Properties pro = new Properties();            InputStream in = getClass().getResourceAsStream("log.properties");            pro.load(in);            in.close();            String fileName = pro.getProperty("logfile");            out = new PrintWriter(new FileWriter(fileName,true),true);        }catch(Exception e){            e.printStackTrace();        }    }    /*     * 关闭日志     */    public void destroy(){        try{            this.logger = null;            if(out!=null){                out.close();            }        }catch(Exception e){            e.printStackTrace();        }    }}

测试:
创建Test2如下

package com.DesignPattern;public class Test2 {    public void test(){        MyLogSystem logger = MyLogSystem.getLogger();        logger.log("test2");    }}

创建Test1

package com.DesignPattern;public class Test1 {    public static void main(String[] aegs){        MyLogSystem logger = MyLogSystem.getLogger();        logger.log("test1");        Test2 test = new Test2();        test.test();    }}

结果:只输出一个”New Log!”

0 0
原创粉丝点击