美文网首页
设计模式-创建型模式-单例模式

设计模式-创建型模式-单例模式

作者: 小小小8021 | 来源:发表于2018-11-27 00:09 被阅读7次

设计模式-创建型模式-单例模式

创建型模式

创建型模式隐藏类的实例和创建细节,通过隐藏对象如何创建组合在一起达到整个系统独立。

单例模式

确保同一时刻只有一个实例被访问。

Ensure a class has only one instance, and provide a global point of access to it. 确保某一个类只有一个实例,并且自行实例化并向整个系统提供这个实例。

类图

设计模式-创建型模式-单例模式

痴汉模式

在运行的时候直接加载实例化对象

<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 {
// 在一加载的时候直接加载
private final static Singleton singleton = new Singleton();
// 确保不会被实例化
private Singleton() {

}
// 其他方法,建议使用static
public static Singleton getSingleton() {
    return singleton;
}

}

package demo2;

public class Test {
public static void main(String[] args) {
Singleton.getSingleton();
}
}

</pre>

缺点

使用这个会造成在未使用的时候,出现大量的内存占用。

懒汉模式

即,在使用的时候实例化对象。

<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 {
// 在一加载的时候直接加载
private static Singleton singleton;
// 确保不会被实例化
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>

关于多线程

当在多线程的时候,由于不是final,会造成出现多个实例化对象。使用同步锁。

<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 {
// 在一加载的时候直接加载
private static Singleton singleton;
// 确保不会被实例化
private Singleton() {
if (Singleton.singleton == null) {
synchronized(this) { // 同步
// 如果此时已经有实例化对象,则不需要再次生成实例化对象
if (Singleton.singleton == null) {
Singleton.singleton = new Singleton();
}
}
}
}
// 其他方法,建议使用static
public static Singleton getSingleton() {
return singleton;
}
}

</pre>

应用

web页面计数器,此时使用单例模式

访问IO和数据库资源的时候,使用单例模式

工具类,使用单例模式

数据库的主键

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;
}
// 构造
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);

实例
</pre>

制作一个登陆弹窗的登录界面

一个类,该类为登录框,构造方法里,第一次点击,构造出登录对象,第二次点击,不构造,使用的是单例模式,并设置几个方法,为显示方法,关闭方法。最后绑定事件。

www.iming.info

点击查看更多内容

相关文章

  • 单例模式

    单例 单例模式,是一种设计模式,属于创建型设计模式,还有一种创建型设计模式,工厂模式。设计模式总共有23种,三大类...

  • 开发之设计模式-单例模式

    设计模式 设计模式分为三大类:创建型、结构型、行为型在Java中有24中设计模式 创建型:单例 1、为什么用单例模...

  • 设计模式分类

    经典23种设计模式: 创建型设计模式: Singleton Pattern(单例模式) PrototypePatt...

  • Python 之单例模式

    简介:单例模式(Singleton Pattern) 是最简单的设计模式之一,属于创建型的设计模式。单例模式涉及到...

  • “Python的单例模式有四种写法,你知道么?”——孔乙己

    什么是单例模式 单例模式(Singleton Pattern)是最简单的设计模式之一。这种类型的设计模式属于创建型...

  • 设计模式简单总结(待完善)

    设计模式简单总结 设计模式可以分为:创建型,结构型,行为型三种模式。 1 创建型模式 1.1 单例模式 用来指定某...

  • PHP设计模式—创建型设计模式

    ** 创建型设计模式 **: 单例模式(Singleton Pattern) 工厂方法模式(Factor Patt...

  • S4. 单例模式

    单例模式(Singleton) 介绍 单例模式是创建型设计模式,即用于创建对象的设计。其能够保证当前系统仅存在一个...

  • 【设计模式】创建型设计模式汇总

    创建型设计模式汇总 1. 单例模式 1.1 单例模式的定义 一个类只允许创建一个对象或实例。 1.2 单例模式的作...

  • android常用设计模式

    26种设计模式 创建型设计模式[5] 单例模式,工厂模式,抽象工厂模式,建造模式,原型模式,[简单工厂模式] 结构...

网友评论

      本文标题:设计模式-创建型模式-单例模式

      本文链接:https://www.haomeiwen.com/subject/jckwqqtx.html