美文网首页
单例模式-恶汉

单例模式-恶汉

作者: togeek | 来源:发表于2017-01-07 21:58 被阅读0次

    Employee.java

    package com.togeek;
    
    import java.util.Date;
    
    public class Employee {
    
        public Employee() {
            // TODO Auto-generated constructor stub
        }
        public static void main(String[] arg) throws InterruptedException{
            System.out.println("现在时间"+new Date().toLocaleString());
            Thread.sleep(1000); 
            Boss.getBoss().say();
            Thread.sleep(3000);     
            Boss.getBoss().say();
        }
    
    }
    
    

    Boss.java

    package com.togeek;
    
    import java.util.Date;
    
    /**
     *这是一个大BOSS应该就有一个 
     */
    public class Boss {
        private static final Boss boss=new Boss();//这boss进来就有了
        private Boss() {}//私有的不能new
        private Date sayTime=null;
        public static Boss getBoss(){
            return boss;
        }
        public void say(){
            if(null==sayTime)sayTime=new Date();
            System.out.println("boss说"+sayTime.toLocaleString()+"放假");
        }
    
    }
    

    相关文章

      网友评论

          本文标题:单例模式-恶汉

          本文链接:https://www.haomeiwen.com/subject/oyofbttx.html