設(shè)計(jì)模式-創(chuàng)建型模式-單例模式
創(chuàng)建型模式
創(chuàng)建型模式隱藏類的實(shí)例和創(chuàng)建細(xì)節(jié),通過(guò)隱藏對(duì)象如何創(chuàng)建組合在一起達(dá)到整個(gè)系統(tǒng)獨(dú)立猛遍。
單例模式
確保同一時(shí)刻只有一個(gè)實(shí)例被訪問(wèn)。
Ensure a class has only one instance, and provide a global point of access to it. 確保某一個(gè)類只有一個(gè)實(shí)例援奢,并且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例驻债。
類圖
癡漢模式
在運(yùn)行的時(shí)候直接加載實(shí)例化對(duì)象
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">package demo2;
// 演示單例模式
public class Singleton {
// 在一加載的時(shí)候直接加載
private final static Singleton singleton = new Singleton();
// 確保不會(huì)被實(shí)例化
private Singleton() {
}
// 其他方法,建議使用static
public static Singleton getSingleton() {
return singleton;
}
}
package demo2;
public class Test {
public static void main(String[] args) {
Singleton.getSingleton();
}
}
</pre>
缺點(diǎn)
使用這個(gè)會(huì)造成在未使用的時(shí)候强衡,出現(xiàn)大量的內(nèi)存占用擦秽。
懶漢模式
即,在使用的時(shí)候?qū)嵗瘜?duì)象漩勤。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">package demo2;
// 演示單例模式
public class Singleton {
// 在一加載的時(shí)候直接加載
private static Singleton singleton;
// 確保不會(huì)被實(shí)例化
private Singleton() {
if (Singleton.singleton == null)
Singleton.singleton = new Singleton();
}
// 其他方法感挥,建議使用static
public static Singleton getSingleton() {
return singleton;
}
}
package demo2;
public class Test {
public static void main(String[] args) {
Singleton.getSingleton();
}
}
</pre>
關(guān)于多線程
當(dāng)在多線程的時(shí)候,由于不是final越败,會(huì)造成出現(xiàn)多個(gè)實(shí)例化對(duì)象触幼。使用同步鎖。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">package demo2;
// 演示單例模式
public class Singleton {
// 在一加載的時(shí)候直接加載
private static Singleton singleton;
// 確保不會(huì)被實(shí)例化
private Singleton() {
if (Singleton.singleton == null) {
synchronized(this) { // 同步
// 如果此時(shí)已經(jīng)有實(shí)例化對(duì)象究飞,則不需要再次生成實(shí)例化對(duì)象
if (Singleton.singleton == null) {
Singleton.singleton = new Singleton();
}
}
}
}
// 其他方法置谦,建議使用static
public static Singleton getSingleton() {
return singleton;
}
}
</pre>
應(yīng)用
web頁(yè)面計(jì)數(shù)器堂鲤,此時(shí)使用單例模式
訪問(wèn)IO和數(shù)據(jù)庫(kù)資源的時(shí)候,使用單例模式
工具類媒峡,使用單例模式
數(shù)據(jù)庫(kù)的主鍵
js單例模式
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232);">var Singleton = function(name){
this.name = name;
}
// 添加方法
Singleton.prototype.getName = function(){
return this.name;
}
// 構(gòu)造
Singleton.getSingleton = function(name){
if (!Singleton.instace){
this.instace = new Singleton();
}
return this.instace;
}
es6單例模式
class Singleton{
constructor(){
this.name = "";
}
static getSingleton(){
if(!this.instance){
this.instance = new Singleton();
}
return this.instance;
}
getName(){
return this.name;
}
}
let a = Singleton.getSingleton();
let b = Singleton.getSingleton();
console.log(a === b);
實(shí)例
</pre>
制作一個(gè)登陸彈窗的登錄界面
一個(gè)類瘟栖,該類為登錄框,構(gòu)造方法里谅阿,第一次點(diǎn)擊半哟,構(gòu)造出登錄對(duì)象,第二次點(diǎn)擊签餐,不構(gòu)造镜沽,使用的是單例模式,并設(shè)置幾個(gè)方法贱田,為顯示方法缅茉,關(guān)閉方法。最后綁定事件男摧。
點(diǎn)擊查看更多內(nèi)容