单子模式

饿汉模式
不管是否为空先new出一个对象
1·单例模式不能让其自动实例化,所以要将构造器私有化,使其不能直接通过new的方式创建对象
2·通过new在代码内部创建一个实例对象
3·定义一个public static的共有静态方法,返回上一步中创建的实例对象;由于在静态方法中,所以上一步的对象也是static

package com.helan.a;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;

//单子模式饿汉模式
public class SingletonImpl {
    private SingletonImpl(){
        //1,声明一个私有构造器
    }
    private static SingletonImpl singleton=new SingletonImpl();
    //实例化一个静态自己本身对象

    public static SingletonImpl getInstance(){
        return singleton;
    }

    public static void main(String[] args) {
        SingletonImpl S=SingletonImpl.getInstance();
        SingletonImpl S2=SingletonImpl.getInstance();
        System.out.println(S);
        System.out.println(S2);
    }
}

懒汉模式
判断对象是否为空,为空的时候才进行实例化
1·声明私有构造器
2·声明一个自己本身对象
3·声明一个对象后并不马上对其实例化,而是进行判断,为空的时候才进行实例化

package com.helan.a;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;

//单子模式懒汉模式
public class SingletonImpl {
    private SingletonImpl(){
        //1,声明一个私有构造器
    }
    private static SingletonImpl singleton;
    //声明一个静态自己本身对象

    public static SingletonImpl getInstance(){
        if(singleton==null){
            singleton=new SingletonImpl();
        }
        return singleton;
    }

    public static void main(String[] args) {
        SingletonImpl S=SingletonImpl.getInstance();
        SingletonImpl S2=SingletonImpl.getInstance();
        System.out.println(S);
        System.out.println(S2);
    }
}

多线程同步锁
为保证多线程下实例只被创建一次,需要加同步锁

1·声明私有构造器
2·声明一个自己本身对象
3·在判断实例是否为空的基础上增加同步锁,同步锁要对当前类的运行实类进行锁定

package com.helan.a;



//单子模式多线程同步锁
public class SingletonImpl {
    private SingletonImpl(){
        //1,声明一个私有构造器
    }
    private static SingletonImpl singleton;
    //声明一个静态自己本身对象

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

    public static void main(String[] args) {
        SingletonImpl S=SingletonImpl.getInstance();
        SingletonImpl S2=SingletonImpl.getInstance();
        System.out.println(S);
        System.out.println(S2);
    }
}

同步锁改进
为了保证实例为new时,只new出一个实例,在这个时候加锁

package com.helan.a;



//单子模式多线程同步锁改进
public class SingletonImpl {
    private SingletonImpl(){
        //1,声明一个私有构造器
    }
    private static SingletonImpl singleton;
    //声明一个静态自己本身对象

    public static SingletonImpl getInstance(){

        if(singleton==null){
            synchronized (SingletonImpl.class){
            singleton=new SingletonImpl();
        } }
        return singleton;
   }

    public static void main(String[] args) {
        SingletonImpl S=SingletonImpl.getInstance();
        SingletonImpl S2=SingletonImpl.getInstance();
        System.out.println(S);
        System.out.println(S2);
    }
}

静态代码块
根据jdk的特性
在Java中静态代码块只会在用到该类的时候被调用唯一的一次

1·声明私有构造器
2·声明一个自己本身对象
3·在静态代码块中实例化对象
4·getInstance返回自己本身

package com.helan.a;



//单子模式静态代码块
public class SingletonImpl {
    private SingletonImpl(){
        //1,声明一个私有构造器
    }
    private static SingletonImpl singleton;
    //声明一个静态自己本身对象
    static {//本身就是线程同步且仅执行一次
        singleton=new SingletonImpl();
    }

    public static SingletonImpl getInstance(){


        return singleton;
   }

    public static void main(String[] args) {
        SingletonImpl S=SingletonImpl.getInstance();
        SingletonImpl S2=SingletonImpl.getInstance();
        System.out.println(S);
        System.out.println(S2);
    }
}

静态代码块按需创建
只有调用getInstance静态方法时,实例才会被创建,
调用其它静态方法或者其他任何时候都不会创建实例对象,
即按照需求在合适的时间被创建

1·声明私有构造器
2·专门用于创建Singleton的静态类//私有
3·返回静态类下的实例对象

package com.helan.a;
//单子模式静态代码块按需创建
public class SingletonImpl {
    private SingletonImpl(){
        //1,声明一个私有构造器
    }
    private static class Nested{
        private static SingletonImpl singleton;
        static {
            singleton=new SingletonImpl();
        }

    }
    public static SingletonImpl getInstance(){


        return Nested.singleton;
   }

    public static void main(String[] args) {
        SingletonImpl S=SingletonImpl.getInstance();
        SingletonImpl S2=SingletonImpl.getInstance();
        System.out.println(S);
        System.out.println(S2);
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值