黑马程序员技术交流社区
标题:
关于单例模式的问题
[打印本页]
作者:
liqinwyyx
时间:
2014-12-4 13:22
标题:
关于单例模式的问题
懒汉式单例、饿汉式单例、登记式单例三种之间到底有什么区别?求大神代码解说。
作者:
liqinwyyx
时间:
2014-12-4 13:41
懒汉式:
public class Test6 {
static class Animal {
private static Animal instance = null;
private String name;
private String color;
private int height;
private Animal(String name, String color, int height) {
super();
this.name = name;
this.color = color;
this.height = height;
}
//构造函数私有,实例化对象只在本类范围内
private Animal() {
}
//拿到单例的方法静态
public static Animal GetInstance() {
//只有实例为空时,即需要创建一个实例时,才需加锁创建
//若已存在一个实例,就直接返回该实例。
if (instance == null) {
//无法保证线程安全。当有多个线程同时访问getInstance的时候,此时若对象为空,
//就会出现会多个线程同时产生多个Singleton对象,故加锁。
synchronized (Animal.class) {
if (instance == null) {
//在方法中对声明的对象进行实例化,在判断条件成立的前提下,只要类一加载
//对象不会实例化,必须调用获得对象的方法才能实例化此对象,这就是延迟加载。
instance = new Animal();
}
}
}
return instance;
}
@Override
public String toString() {
return "名字:" + name + " 颜色: " + color + " 身高:" + height;
}
}
public static void main(String[] args) throws Exception {
Animal a1 = Animal.GetInstance();
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
throw new Exception("出错啦...");
}
Animal a2 = Animal.GetInstance();
System.out.println(a1 == a2);
a1 = new Animal("长颈鹿", "黄色", 210);
a2 = new Animal("熊猫", "黑色", 140);
System.out.println(a1 == a2);
System.out.println(a1);
System.out.println(a2);
}
}
饿汉式:public class Test7 {
static class People {
private static People instance = new People();
private String name;
private String sex;
private int age;
private People(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
//构造函数私有,实例化对象只在本类范围内
private Animal() {
}
//拿到单例的方法静态
public static People GetInstance() {
return instance;
}
@Override
public String toString() {
return "姓名:" + name + " 性别: " + sex + " 年龄:" + age;
}
}
public static void main(String[] args) throws Exception {
People a1 = People.GetInstance();
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
throw new Exception("出错啦...");
}
People a2 = People.GetInstance();
System.out.println(a1 == a2);
a1 = new People("张飞", "男", 23);
a2 = new People("貂蝉", "女", 21);
System.out.println(a1 == a2);
System.out.println(a1);
System.out.println(a2);
}
}
登记式:
import java.util.HashMap;
import java.util.Map;
//登记式单例类.
//类似Spring里面的方法,将类名注册,下次从里面直接获取。
public class Singleton3 {
private static Map<String,Singleton3> map = new HashMap<String,Singleton3>();
static{
Singleton3 single = new Singleton3();
map.put(single.getClass().getName(), single);
}
//保护的默认构造子
protected Singleton3(){}
//静态工厂方法,返还此类惟一的实例
public static Singleton3 getInstance(String name) {
if(name == null) {
name = Singleton3.class.getName();
System.out.println("name == null"+"--->name="+name);
}
if(map.get(name) == null) {
try {
map.put(name, (Singleton3) Class.forName(name).newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return map.get(name);
}
//一个示意性的商业方法
public String about() {
return "Hello, I am RegSingleton.";
}
public static void main(String[] args) {
Singleton3 single3 = Singleton3.getInstance(null);
System.out.println(single3.about());
}
}
作者:
清诗纪事
时间:
2014-12-4 20:38
只知道懒汉式和饿汉式
区别就是:饿汉式是类一进内存,就创建对象的
懒汉式要方法被调用时,才创建对象
饿汉式:
class Single
{
private Single(){}
private static Single s=new Single();
public static Single getInstance(){
return s;
}
}
复制代码
懒汉式:
class Single
{
private Single(){}
private static s=null;
public static Single getInstance(){
if(s==null){
synchronized(Single.class){
if(s==null)
s=new Single();
}
}
return s;
}
}
复制代码
作者:
zmhlnrs
时间:
2014-12-4 23:01
饿汉式存在就有对象,而懒汉式是调用者创建对象。
登记式不清楚
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2