//Then they all shouted again, saying, "Not this man, but Barabbas!" Now Barabbas was a robber (John 18:40) package com.javarush.task.task20.task2008; import java.io.*; /* Ðак ÑеÑиализоваÑÑ Singleton? */ public class Solution implements Serializable { public static void main(String[] args) throws IOException, ClassNotFoundException { Singleton instance = Singleton.getInstance(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //Serializing the singleton instance ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream); oos.writeObject(instance); oos.close(); //Recreating the instance by reading the serialized object data add ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream); Singleton singleton = (Singleton) ois.readObject(); ois.close(); //Recreating the instance AGAIN by reading the serialized object data add byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ObjectInputStream ois2 = new ObjectInputStream(byteArrayInputStream); Singleton singleton1 = (Singleton) ois2.readObject(); ois2.close(); //The singleton behavior has been broken System.out.println("Instance reference check : " + singleton.getInstance()); System.out.println("Instance reference check : " + singleton1.getInstance()); System.out.println("========================================================="); System.out.println("Object reference check : " + singleton); System.out.println("Object reference check : " + singleton1); } public static class Singleton implements Serializable { private static Singleton ourInstance; public static Singleton getInstance() { if (ourInstance == null) { ourInstance = new Singleton(); } return ourInstance; } private Singleton() { } private Object readResolve() throws ObjectStreamException { return getInstance(); } } } /* Ðак ÑеÑиализоваÑÑ Singleton? Ðва деÑеÑиализованнÑÑ Ð¾Ð±ÑекÑа singleton и singleton1 имеÑÑ ÑазнÑе ÑÑÑлки в памÑÑи, а Ð´Ð¾Ð»Ð¶Ð½Ñ Ð¸Ð¼ÐµÑÑ Ð¾Ð´Ð¸Ð½Ð°ÐºÐ¾Ð²Ñе. РклаÑÑ Singleton Ð´Ð¾Ð±Ð°Ð²Ñ Ð¾Ð´Ð¸Ð½ меÑод (погÑглиÑе), ÑÑÐ¾Ð±Ñ Ð¿Ð¾Ñле деÑеÑиализаÑии ÑÑÑлки на обÑекÑÑ Ð±Ñли ÑавнÑ. ÐеÑод main не ÑÑаÑÑвÑÐµÑ Ð² ÑеÑÑиÑовании. ТÑебованиÑ: 1.âÐлаÑÑ Solution.Singleton должен поддеÑживаÑÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ Serializable. 2.âРклаÑÑе Solution.Singleton должен бÑÑÑ Ñеализован меÑод readResolve без паÑамеÑÑов. 3.âÐеÑод readResolve должен возвÑаÑаÑÑ ÑинглÑон (ourInstance). 4.âÐеÑод readResolve должен бÑÑÑ Ð¿ÑиваÑнÑм. package com.javarush.task.task20.task2008; import java.io.Serializable; * Ðак ÑеÑиализоваÑÑ Singleton? * public class Solution implements Serializable { public static void main(String[] args) throws IOException, ClassNotFoundException { Singleton instance = Singleton.getInstance(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //Serializing the singleton instance ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream); oos.writeObject(instance); oos.close(); //Recreating the instance by reading the serialized object data add ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream); Singleton singleton = (Singleton) ois.readObject(); ois.close(); //Recreating the instance AGAIN by reading the serialized object data add byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ObjectInputStream ois2 = new ObjectInputStream(byteArrayInputStream); Singleton singleton1 = (Singleton) ois2.readObject(); ois2.close(); //The singleton behavior has been broken System.out.println("Instance reference check : " + singleton.getInstance()); System.out.println("Instance reference check : " + singleton1.getInstance()); System.out.println("========================================================="); System.out.println("Object reference check : " + singleton); System.out.println("Object reference check : " + singleton1); } public static class Singleton implements Serializable { private static Singleton ourInstance; public static Singleton getInstance() { if (ourInstance == null) { ourInstance = new Singleton(); } return ourInstance; } private Singleton() { } } } */