单例模式-代码示例

package com.example.springbootdemo.designpattern.singleton;

/**
* 单例模式实现
* 适用于单线程
*
* @author wentao
* @time 20190318
* @copyright Gods bless me,code never with bug.
*/
public class SingletonBySingleThread {
private static SingletonBySingleThread instance = null;

private SingletonBySingleThread() {

}

public static SingletonBySingleThread getInstance() {
if (null == instance) {
instance = new SingletonBySingleThread();
}
return instance;
}
}


******************************************************************************

package com.example.springbootdemo.designpattern.singleton;

/**
* 单例模式
* 懒汉式加载
*
* @author wentao
* @time 20190318
* @copyright Gods bless me,code never with bug.
*/
public class SingletonByLazy {
private static SingletonByLazy instance = null;

private SingletonByLazy() {

}

/**
* 此创建方法运行效率较低,用synchronized加锁解锁会比较耗时
*
* @param
* @return com.example.springbootdemo.designpattern.singleton.SingletonByLazy
* @throws
* @author wentao
* @time 20190318
* Gods bless me,code never with bug.
*/
public static synchronized SingletonByLazy getInstance() {
if (null == instance) {
instance = new SingletonByLazy();

}
return instance;
}

/**
* 此创建方法相对上一个方法,效率有所提升。只用synchronized包裹住实例不存在创建的过程
* 相比于上一个方法来说,减少了实例存在的情况下直接返回的那部分效率
*
* @param
* @return com.example.springbootdemo.designpattern.singleton.SingletonByLazy
* @throws
* @author wentao
* @time 20190318
* Gods bless me,code never with bug.
*/
public static SingletonByLazy getInstanceOtherWay() {
if (null == instance) {
synchronized (SingletonByLazy.class) {
if (null == instance) {
instance = new SingletonByLazy();
}
}

}
return instance;
}
}


***************************************************************************************************

package com.example.springbootdemo.designpattern.singleton;

/**
* 单例模式-饿汉式加载
*
* @author wentao
* @time 20190318
* @copyright Gods bless me,code never with bug.
*/
public class SingletonByHungry {

private static SingletonByHungry instance = new SingletonByHungry();
private static String name = "饿汉式加载创建";

private SingletonByHungry() {
}

public static SingletonByHungry getInstance() {
return instance;
}

public void printCreaterName() {
System.out.println("该类创建者是" + name);
}

}

******************************************************************************************

package com.example.springbootdemo.designpattern.singleton;

/**
* 单例模式-声明式
* 即采用内部类形式创建
*
* @author wentao
* @time 20190318
* @copyright Gods bless me,code never with bug.
*/
public class SingletonByDeclarative {

private SingletonByDeclarative() {

}

private static class Singleton {
private final static SingletonByDeclarative instance = new SingletonByDeclarative();
}

public static SingletonByDeclarative getInstance() {
return Singleton.instance;
}
}

*****************************************************************************************
package com.example.springbootdemo.designpattern.singleton;

/**
* 单例模式测试类
*
* @author wentao
* @time 20190318
* @copyright Gods bless me,code never with bug.
*/
public class SingletonTest {

public static class ThreadTest implements Runnable{
@Override
public void run() {
System.out.println( SingletonByLazy.getInstance());
}
}

public static void main(String[] args) {
//创建多个线程,每个线程获取一个单例模式的对象,并调用该对象的方法输出语句
for (int i=0;i<10;i++){
ThreadTest threadTest=new ThreadTest();
new Thread(threadTest).start();
}
}

}
 
 
 
posted @ 2019-03-18 14:25  穷逼苦行僧  阅读(747)  评论(0编辑  收藏  举报