单例设计模式

package com.yunchao.test;
/**
 * 单例设计模式
 * 1.将构造函数私有化,这样其他的类就不能new出来这个类的对象
 * 2.自己在类里面new出来一个对象
 * 3.把new出来的对象提供出去,应为其他对象不能够通过对象来调用暴露的方法,所以方法应该
 * 设计成static的,通过类名来调用,然后就是static方法里的变量也要是static的
 * @author yunchao
 *
 */
public class Singleton {
 public static void main(String[] args) {
  single s1=single.getsingle();
 }
}

class single{
 private single(){}
 private static single s=new single();
 public  static single getsingle(){
  return s;
 }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章