单例模式双重锁,懒汉饿汉,还有冒泡排序,小白必备技能

比较安全:
public class SingletonInstance {
//私有构造方法
private SingletonInstance (){

}
private static class Builder{
    //声明成员变量
    private static SingletonInstance singletonInstance = new SingletonInstance();
}
//对外提供接口获取该实例
public static SingletonInstance getSingletonInstance(){
    return Builder.singletonInstance ;
}

}

懒汉式:
public class Singleton{
private static Singleton singleton = null;
public static synchronized getInstance(){
if(singleton==null){
singleton = new Singleton();
}
return singleton;
}
}

饿汉式:
public class Singleton{
private static Singleton singleton = new Singleton ();
private Singleton (){
}
public Singleton getInstance(){
return singletion;
}
}

冒泡排序:
 public static void main(String[] args) {
    int[] arr={6,3,8,2,9,1};
    for(int i=0;i<arr.length-1;i++){//外层循环控制排序趟数
      for(int j=0;j<arr.length-1-i;j++){//内层循环控制每一趟排序多少次
        if(arr[j]>arr[j+1]){
          int temp=arr[j];
          arr[j]=arr[j+1];
          arr[j+1]=temp;
        }
      }
    }
    for(int num:arr){
      System.out.print(num+" ");
    }
  }

上一篇:C++ 设计模式(二)单例模式、策略模式、责任链模式和装饰器模式


下一篇:设计模式之单例