设计模式 -- 单例模式

参考:http://www.runoob.com/design-pattern/singleton-pattern.html
这里写图片描述
源码下载:https://github.com/gpqhl0071/pattern.git
1. 对象在整个工程的运行周期内,值创建一个对象
这里写图片描述
代码

/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;

public class SingleObject {

    // 创建 SingleObject 的一个对象
    private static SingleObject instance = new SingleObject();

    // 让构造函数为 private,这样该类就不会被实例化
    private SingleObject() {
    }

    // 获取唯一可用的对象
    public static SingleObject getInstance() {
        return instance;
    }

    public void showMessage() {
        System.out.println("Hello World!");
    }
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single.test;

import com.pattern.single.SingleObject;

public class SingletonPatternDemo {
    public static void main(String[] args) {

        // 不合法的构造函数
        // 编译时错误:构造函数 SingleObject() 是不可见的
        // SingleObject object = new SingleObject();

        // 获取唯一可用的对象
        SingleObject object = SingleObject.getInstance();

        // 显示消息
        object.showMessage();
    }
}

结果:

Hello World!

6中单例的实现,代码如下:

/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;

/**
 * 懒汉式,线程不安全
 * @author: gao peng
 * @date:   2016年3月3日 上午11:33:16   
 *
 */
public class Singleton1 {
    private static Singleton1 instance;

    private Singleton1() {
    }

    public static Singleton1 getInstance() {
        if (instance == null) {
            instance = new Singleton1();
        }
        return instance;
    }
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;

/**
 * 懒汉式,线程安全
 * @author: gao peng
 * @date:   2016年3月3日 上午11:34:11   
 *
 */
public class Singleton2 {
    private static Singleton2 instance;

    private Singleton2 (){}

    public static synchronized Singleton2 getInstance() {
        if (instance == null) {
            instance = new Singleton2();
        }
        return instance;
    }
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;

/**
 * 饿汉式
 * @author: gao peng
 * @date:   2016年3月3日 上午11:34:39   
 *
 */
public class Singleton3 {
    private static Singleton3 instance = new Singleton3();

    private Singleton3() {
    }

    public static Singleton3 getInstance() {
        return instance;
    }
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;

/**
 * 双检锁/双重校验锁(DCL,即 double-checked locking)
 * @author: gao peng
 * @date:   2016年3月3日 上午11:35:21   
 *
 */
public class Singleton4 {
    private volatile static Singleton4 Singleton4;

    private Singleton4() {
    }

    public static Singleton4 getSingleton4() {
        if (Singleton4 == null) {
            synchronized (Singleton4.class) {
                if (Singleton4 == null) {
                    Singleton4 = new Singleton4();
                }
            }
        }
        return Singleton4;
    }
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;

/**
 * 登记式/静态内部类
 * @author: gao peng
 * @date:   2016年3月3日 上午11:36:20   
 *
 */
public class Singleton5 {
    private static class Singleton5Holder {
        private static final Singleton5 INSTANCE = new Singleton5();
    }

    private Singleton5() {
    }

    public static final Singleton5 getInstance() {
        return Singleton5Holder.INSTANCE;
    }
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;

/**
 * 枚举
 * @author: gao peng
 * @date:   2016年3月3日 上午11:37:07   
 *
 */
public enum Singleton6 {
    INSTANCE;  
    public void whateverMethod() {  
    } 

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值