单例模型的几种实现方式

一、懒汉模式

二、饿汉模式

三、枚举模式

单例模型

保证在内存中有给定数量的对象。

eg.内存中产生唯一一个对象。—国无二主

问题分析

防止外界创建对象

Car c = new Car();

将构造器隐藏起来,防止外界调用。

只初始化、创建一次

static: 类加载的时候初始化一次。

Method区,类加载 Car.class —》类加载器——>内存: 初始化这块空间

懒汉模式

 
  1. private static Car instance; //instance没有引用对象,
  2. private Car() {
  3. }
  4. public static Car getInstance() { //懒加载
  5. if (Objects.isNull(instance)) {
  6. instance = new Car();
  7. }
  8. return instance;
  9. }

懒汉的困惑

 
  1. package com.wnhz.singleton.entity;
  2. import java.util.Objects;
  3. public class Car {
  4. private static Car instance; //instance没有引用对象,
  5. private Car() {
  6. }
  7. public static synchronized Car getInstance() { //懒加载
  8. if (Objects.isNull(instance)) {
  9. instance = new Car();
  10. }
  11. return instance;
  12. }
  13. }
  1. 读数据 a
  2. 写数据 b
  3. 读 c
  4. 写 d

================

1.读数据 a

 
 
  1. 2. 读 c
  2. 3. 写数据 b
  3. 4. 写数据 d
 
  1. public static Car getInstance() { //懒加载
  2. if (Objects.isNull(instance)) {
  3. synchronized (Car.class){
  4. if(Objects.isNull(instance)){
  5. instance = new Car();
  6. }
  7. }
  8. }
  9. return instance;
  10. }

可重入锁

 
  1. public class Car2 {
  2. private final static ReentrantLock lock = new ReentrantLock();
  3. private static Car2 instance; //instance没有引用对象,
  4. private Car2() {
  5. }
  6. public static Car2 getInstance() { //懒加载
  7. try{
  8. lock.lock();
  9. if(Objects.isNull(instance)){
  10. instance = new Car2();
  11. }
  12. }finally {
  13. lock.unlock();
  14. }
  15. return instance;
  16. }
  17. }

饿汉模式

 
  1. public class Car3 {
  2. private static Car3 instance = new Car3();
  3. private Car3(){}
  4. public static Car3 getInstance() {
  5. return instance;
  6. }
  7. }

枚举类型

引子

隐藏在mybatisplus中的枚举类。

 
  1. public enum IdType {
  2. AUTO(0),
  3. NONE(1),
  4. INPUT(2),
  5. ASSIGN_ID(3),
  6. ASSIGN_UUID(4);
  7. private final int key;
  8. private IdType(int key) {
  9. this.key = key;
  10. }
  11. public int getKey() {
  12. return this.key;
  13. }
  14. }

AUTO 表示数字 0 ——>mysql数据库的自增长

产生目的

让我们的项目中的常量可读性增强。

“魔鬼”常量

 
  1. if(a == Login.ONE_TIME){
  2. }
  3. x = 1;

枚举的语法

 
  1. public enum Weather {
  2. 晴天,雨天 ,雪天,阴天
  3. }
 
  1. public final class Weather extends java.lang.Enum<Weather> {
  2. public static final Weather SUN;
  3. public static final Weather RAIN;
  4. public static final Weather SNOW;
  5. public static final Weather CLOUD;
  6. public static Weather[] values();
  7. public static Weather valueOf(java.lang.String);
  8. static {};
  9. }

枚举是一个特殊的类

 
  1. public enum WeatherEnum {
  2. SUNNY(1,"晴天"),
  3. SNOWING(2,"雪天"),
  4. ;
  5. private int code;
  6. private String msg;
  7. WeatherEnum(int code, String msg) {
  8. this.code = code;
  9. this.msg = msg;
  10. }
  11. public int getCode(){
  12. return this.code;
  13. }
  14. public String getMsg(){
  15. return this.msg;
  16. }
  17. }

枚举的单例解决方案

 
  1. public enum Singleton {
  2. INSTANCE;
  3. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值