这里用了新学的builder模式构建对象,在一个类内部创建一个静态内部类Builder。
Builder持有和外部类一样的资源,Builder可以无参或者参数构造,有每个资源的名字为方法名set方法,还拥有builder方法返回一个外部类对象,这里返回调用的是外部类的private有参构造方法。
加入了单例模式(纯属测试下单例模式),用的是懒汉式创建,有一个private static句柄,构造函数为private防止外部创建,提供了getInstance的静态方法来返回需要的句柄。在这个方法里面,对本类上锁,双重if判断,从而实现线程安全(这里也可以用静态内部类实现或者枚举)。
因而,外部可以用getInstance,或者OuterClass.Builder(args).arg1(100),arg2(200).build()之类的句式,获得外部类对象,但是无论如何,该类对象只有一个单例。
接着试验了自带的序列化方法,直接将外部类implements了Serializable接口,
重写了readResolve方法,返回单例对象防止反序列化时出现非单例。
接着用ObjectOutputStream等流写入文件再读出,打印出结果。
证实了想法。这里没测试transit关键字,我在其他地方已经测试过了,序列化会忽略该关键字。另外在读对象的时候,遇到文件结束会抛出EOF异常,捕获了可以了。
如下所示:
Object obj;
try{while((obj=in.readObject())!=null)System.out.println((OuterClass)obj);
}catch(EOFException e){System.out.println("读取完毕");
}
以下是正文:
package com.crimson.master;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class MultipleTest implements Serializable
{
/**
* @author crimson
* @version v0.1
*/
public static void main(String[] args) throws ClassNotFoundException
{
// TODO Auto-generated method stub
MultipleTest test = MultipleTest.getInstance();
System.out.println(test);
MultipleTest a = null;
MultipleTest test2 = new MultipleTest.Builder().id("anna").grade(5).name("tom").tips("winwin").build();
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("G:\\set-linux\\a.txt")))
{
out.writeObject(test2);
} catch (IOException e)
{
e.printStackTrace();
}
;
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("G:\\\\set-linux\\\\a.txt")))
{
a = (MultipleTest) in.readObject();
} catch (IOException e)
{
e.printStackTrace();
}
;
a.display();
}
private int id;
private String name;
private int grade;
private String tips;
private static MultipleTest holder;
//静态内部类,用于构建实例或者设置参数
public static class Builder
{
//@
private int id;
private String name;
private int grade;
private String tips;
public Builder(int id, String name, int grade, String tips)
{
this.id = id;
this.name = name;
this.grade = grade;
this.tips = tips;
}
public Builder()
{
this(0, "null", 0, "null");
}
public Builder id(int id)
{
this.id = id;
return this;
}
public Builder name(String name)
{
this.name = name;
return this;
}
public Builder grade(int grade)
{
this.grade = grade;
return this;
}
public Builder tips(String tips)
{
this.tips = tips;
return this;
}
public MultipleTest build()
{
return MultipleTest.getInstance().setBuilder(this);
}
}
private MultipleTest(Builder b)
{
this.grade = b.grade;
this.id = b.grade;
this.name = b.name;
this.tips = b.tips;
}
private MultipleTest()
{
}
;
public MultipleTest setBuilder(Builder b)
{
this.grade = b.grade;
this.id = b.grade;
this.name = b.name;
this.tips = b.tips;
return this;
}
//获取实例两种方法一种是getInstance,一种是构建一个Builder后调用其build
public static MultipleTest getInstance()
{
if (MultipleTest.holder == null)
{
synchronized (MultipleTest.class)
{
if (MultipleTest.holder == null)
{
MultipleTest.holder = new MultipleTest(new Builder());
}
}
}
return MultipleTest.holder;
}
@Override
public String toString()
{
return "id:" + id + " name:" + name + " grade:" + grade + " tips:" + tips + "\n";
}
private Object readResolve()
{
return MultipleTest.holder;
}
public void display()
{
System.out.println(this);
}
}