2021-08-24

单例模式5种分类

1. 饿汉模式
2. 懒汉模式
3. 双重检查锁模式
4. 静态内部类模式
5. 枚举模式

模式具体实现

  1. 饿汉模式
public class SingletonDemo01 {
	private static SingletonDemo01 instance = new SingletonDemo01();
	private SingletonDemo01() {
	}
	
	public static SingletonDemo01 getInstance() {
		return instance;
	}
}
  1. 懒汉模式
public class SingletonDemo02 {
	private static SingletonDemo02 instance;
	private SingletonDemo02() {
	}
	
	public static synchronized SingletonDemo02 getInstance() {
		if(instance == null) {
			instance = new SingletonDemo02();
		}
		return instance;
	}
}在这里插入代码片
  1. 双重检查锁模式
public class SingletonDemo05 {
	private static SingletonDemo05 instance = null;
	public static SingletonDemo05 getInstance() {
		if(instance == null) {
			SingletonDemo05 sc;
			synchronized(SingletonDemo05.class) {
				sc = instance;
				if(sc == null) {
					synchronized (SingletonDemo05.class) {
						if(sc == null) {
							sc = new SingletonDemo05();
						}
					}
					instance = sc;
				}
			}
		}
		return instance;
	}
	private SingletonDemo05() {
		
	}
}
  1. 静态内部类模式
public class SingletonDemo04 {
	private static class Singleton{
		private static final SingletonDemo04 getInstance = new SingletonDemo04();
	}
	private SingletonDemo04() {
	}
	
	public static SingletonDemo04 getInstance() {
		return Singleton.getInstance;
	}
}
  1. 枚举模式
public enum SingletonDemo03 {
	INSTANCE;
	
//	添加需要的操作
	public void singletonOperation() {
		
	}
	
}

防止反射和序列化

  1. 枚举模式天然可以防止反射和序列化
  2. 防止反射
    在构造方法种抛出异常
	private SingletonDemo6() {
//		防止反射
		if(instance != null) {
			throw new RuntimeException();
		}
	}
  1. 防止序列化
    定义readResolve()方法
	public Object readResolve() throws ObjectStreamException{
		
		return instance;
	}

单例模式选择

  1. 单例对象占用资源少,不需要延时加载时:
    枚举好于饿汉式
  2. 单例对象占用资源大,需要延时加载时:
    静态内部类好于懒汉式
  3. 双重检查锁模式(不建议使用),存在问题
这段代码存在一些语法错误。具体来说: 1. `console.log` 方法中的字符串应该使用引号括起来,例如 `"每页 ${val} 条"` 和 `"当前页: ${val}"`。 2. `toggleContent` 方法应该定义在 `methods` 对象中,并且缺少一个左大括号 `{`。 3. `toggleContent` 方法中的 `console.log` 方法没有使用正确的字符串格式化,应该使用反引号括起来,并在占位符 `${}` 中使用变量名,例如 ``console.log(`当前高度: ${content.style.height}`)``。 4. 在 `v-for` 指令中,应该使用 `v-bind:key` 显式地绑定 `tableData` 数组中每个对象的唯一标识符,例如 `v-for="(item, index) in tableData" v-bind:key="item.number"`。 下面是修正后的代码: ``` <script> let v = new Vue({ el: '#app', data: { value1: '', value2: '', pickerOptions: { shortcuts: [ { text: '今天', onClick(picker) { picker.$emit('pick', new Date()); } }, { text: '昨天', onClick(picker) { const date = new Date(); date.setTime(date.getTime() - 3600 * 1000 * 24); picker.$emit('pick', date); } }, { text: '一周前', onClick(picker) { const date = new Date(); date.setTime(date.getTime() - 3600 * 1000 * 24 * 7); picker.$emit('pick', date); } } ] }, tableData: [ { number: '1', date: '2021-08-01' }, { number: '2', date: '2021-08-01' }, { number: '3', date: '2021-08-01' }, { number: '4', date: '2021-08-01' }, { number: '5', date: '2021-08-01' }, { number: '6', date: '2021-08-01' }, { number: '7', date: '2021-08-01' }, { number: '8', date: '2021-08-01' } ], currentPage1: 5, currentPage2: 5, currentPage3: 5, currentPage4: 4 }, methods: { deleteRow(index, rows) { rows.splice(index, 1); }, handleSizeChange(val) { console.log(`每页 ${val} 条`); }, handleCurrentChange(val) { console.log(`当前页: ${val}`); }, toggleContent() { var content = document.getElementById("content"); var btn = document.getElementById("toggle-btn"); if (content.style.height === "100px") { content.style.height = "auto"; btn.innerHTML = "收起"; } else { content.style.height = "100px"; btn.innerHTML = "展开"; } console.log(`当前高度: ${content.style.height}`); } } }); </script> <table> <thead> <tr> <th>序号</th> <th>日期</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(item, index) in tableData" v-bind:key="item.number"> <td>{{ item.number }}</td> <td>{{ item.date }}</td> <td><button @click="deleteRow(index, tableData)">删除</button></td> </tr> </tbody> </table> <div class="pagination"> <el-pagination background layout="sizes, prev, pager, next, jumper" :current-page.sync="currentPage1" :page-sizes="[5, 10, 20]" :page-size="5" @size-change="handleSizeChange" @current-change="handleCurrentChange" :total="tableData.length" ></el-pagination> </div> <div id="content" style="overflow: hidden; height: 100px;"> 这是一段需要展开的内容。 </div> <button id="toggle-btn" @click="toggleContent">展开</button> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值