Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1289306
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 2996
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-01 18:08:23

2个原则:
There should be only one instance allowed for a class
We should allow global point of access to that single instance

1. 使用反射,reflection 

点击(此处)折叠或打开

  1. package Singleton;

  2. import java.lang.reflect.Constructor;


  3. //using Reflection we can set the private constructor to become accessible
  4. //at runtime

  5. public class SingletonDemo2{
  6.     //early initialization
  7.     private static SingletonDemo2 instance = new SingletonDemo2();

  8.     private SingletonDemo2(){
  9.         System.out.println("Creating .... ");
  10.         if(instance !=null){
  11.             throw new RuntimeException("Can't create instance,please use get instance");
  12.         }
  13.     }

  14.     public static SingletonDemo2 getInstance(){
  15.         return instance;
  16.     }


  17.     public static void main(String[] args) throws Exception{
  18.         SingletonDemo2 s1 = SingletonDemo2.getInstance();
  19.         SingletonDemo2 s2 = SingletonDemo2.getInstance();

  20.         print("s1", s1);
  21.         print("s2", s2);

  22.         Class clazz= Class.forName("Singleton.SingletonDemo2");
  23.         Constructor<SingletonDemo2> ctor = clazz.getDeclaredConstructor();
  24.         ctor.setAccessible(true);
  25.         SingletonDemo2 s3 = ctor.newInstance();

  26.         print("s3", s3);
  27.     }

  28.     static void print(String name, SingletonDemo2 obj){
  29.         String str;
  30.         str=String.format("Object: %s, Hahscode: %d", name,obj.hashCode());
  31.         System.out.println(str);
  32.     }

  33. }

2.  如果clone 对象的话,则throws Exception

点击(此处)折叠或打开

  1. package Singleton;

  2. import java.lang.reflect.Constructor;

  3. //not allow clone
  4. public class SingletonDemo3 implements Cloneable{
  5.     //early initialization
  6.     private static SingletonDemo3 instance = new SingletonDemo3();

  7.     private SingletonDemo3(){
  8.         System.out.println("Creating .... ");
  9.         if(instance !=null){
  10.             throw new RuntimeException("Can't create instance,please use get instance");
  11.         }
  12.     }

  13.     public static SingletonDemo3 getInstance(){
  14.         return instance;
  15.     }

  16.     @Override
  17.     protected Object clone() throws CloneNotSupportedException{
  18.         if(instance != null) throw new CloneNotSupportedException();
  19.         return super.clone();
  20.     }

  21.     public static void main(String[] args) throws Exception{
  22.         SingletonDemo3 s1 = SingletonDemo3.getInstance();
  23.         SingletonDemo3 s2 = SingletonDemo3.getInstance();

  24.         print("s1", s1);
  25.         print("s2", s2);

  26.         SingletonDemo3 s3= (SingletonDemo3)s2.clone();
  27.         print("s3", s3);
  28.     }

  29.     static void print(String name, SingletonDemo3 obj){
  30.         String str;
  31.         str=String.format("Object: %s, Hahscode: %d", name,obj.hashCode());
  32.         System.out.println(str);
  33.     }

  34. }

3.  允许序列化

点击(此处)折叠或打开

  1. package Singleton;

  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7. import java.lang.reflect.Constructor;

  8. //not allow clone
  9. public class SingletonDemo4 implements Serializable{
  10.     //early initialization
  11.     private static SingletonDemo4 instance = new SingletonDemo4();

  12.     private SingletonDemo4(){
  13.         System.out.println("Creating .... ");
  14.         if(instance !=null){
  15.             throw new RuntimeException("Can't create instance,please use get instance");
  16.         }
  17.     }

  18.     private Object readResolve(){
  19.         System.out.println("applying read resolve");
  20.         return instance;
  21.     }

  22.     public static SingletonDemo4 getInstance(){
  23.         return instance;
  24.     }

  25.     @Override
  26.     protected Object clone() throws CloneNotSupportedException{
  27.         if(instance != null) throw new CloneNotSupportedException();
  28.         return super.clone();
  29.     }

  30.     public static void main(String[] args) throws Exception{
  31.         SingletonDemo4 s1 = SingletonDemo4.getInstance();
  32.         SingletonDemo4 s2 = SingletonDemo4.getInstance();

  33.         print("s1", s1);
  34.         print("s2", s2);

  35.         ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("s2.txt"));
  36.         oos.writeObject(s2);

  37.         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("s2.txt"));
  38.         SingletonDemo4 s3= (SingletonDemo4)ois.readObject();
  39.         print("s3", s3);
  40.     }

  41.     static void print(String name, SingletonDemo4 obj){
  42.         String str;
  43.         str=String.format("Object: %s, Hahscode: %d", name,obj.hashCode());
  44.         System.out.println(str);
  45.     }

  46. }

 
4. 如果允许多线程

点击(此处)折叠或打开

  1. package Singleton;

  2. import java.lang.reflect.Constructor;

  3. public class SingletonDemo implements Cloneable{
  4.     //lazy initialization
  5.     private static SingletonDemo instance =null;

  6.     private SingletonDemo(){
  7.         System.out.println("Creating .... ");
  8.     }

  9.     //in multithreaded environment
  10.     public static synchronized SingletonDemo getInstance(){
  11.     //public static SingletonDemo getInstance(){
  12.         if(instance == null){
  13.             instance = new SingletonDemo();
  14.         }
  15.         return instance;
  16.     }

  17.     @Override
  18.     protected Object clone() throws CloneNotSupportedException{
  19.         //if(instance != null) throw new CloneNotSupportedException();
  20.         return super.clone();
  21.     }

  22.     private Object readResolve(){
  23.         System.out.println("applying read resolve....");
  24.         return instance;
  25.     }

  26.     public static void main(String[] args) throws Exception{
  27.         SingletonDemo s1 = SingletonDemo.getInstance();
  28.         SingletonDemo s2 = SingletonDemo.getInstance();

  29.         print("s1", s1);
  30.         print("s2", s2);

  31.         SingletonDemo s3 = (SingletonDemo)s2.clone();

  32.         print("s3", s3);
  33.     }

  34.     static void print(String name, SingletonDemo obj){
  35.         String str;
  36.         str=String.format("Object: %s, Hahscode: %d", name,obj.hashCode());
  37.         System.out.println(str);
  38.     }

  39. }

阅读(231) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~