Java весна 2013 лекция 7

235 views
204 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
235
On SlideShare
0
From Embeds
0
Number of Embeds
43
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java весна 2013 лекция 7

  1. 1. ObjectOutputStream для этого потока
  2. 2. public class SerializationObject implements Serializable { private String name; // name = Zoe private int age; // age = 31 … }
  3. 3. SerializationObject object = new SerializationObject("Zoe", 31); FileOutputStream fileOut = new FileOutputStream("test.bin"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(object); out.close();
  4. 4. private static final long serialVersionUID = -3895203507200457730L;
  5. 5.
  6. 6.
  7. 7. ― static Class<T> forName(String className) String getCanonicalName() Field getField(String name) Class[] getInterfaces() Method[] getMethods() Constructor[] getConstructors()
  8. 8. getInterfaces()
  9. 9. Bootstrap class loader <JAVA_HOME>/jre/lib Extensions class loader <JAVA_HOME>/jre/lib/ext System class loader CLASSPATH
  10. 10. public static Object createIntance(String className){ try { return Class.forName(className).newInstance(); } catch (…){} }
  11. 11. public static void setFieldValue(Object object, String fieldName, String value){ try { Field field = object.getClass().getDeclaredField(fieldName); field.setAccessible(true); if(field.getType().equals(String.class)){ field.set(object, value); } else if ( field.getType().equals(int.class)){ field.set(object, Integer.decode(value)); } field.setAccessible(false); } catch(…){} }
  12. 12. public class ObjectFactory { public static Object readObject(String path) { try { FileInputStream fileOut = new FileInputStream(path); ObjectInputStream in = new ObjectInputStream(fileOut); Object object = in.readObject(); in.close(); return object; } catch(IOException i) { i.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } }
  13. 13. ― ― ― ―
  14. 14. org.xml.sax.helpers.DefaultHandler startDocument() startElement(…) characters(…) endElement(…) endDocument()
  15. 15. <class type=“main.SerializationObject”> <name>Zully</name> <age>23</age> </class>
  16. 16. public class SaxEmptyHandler extends DefaultHandler { private static String CLASSNAME = "class"; private boolean inElement = false; public void startElement(String uri, String localName, String qName, Attributes attributes) { System.out.println("Start element: " + qName); if(qName != CLASSNAME) inElement = true; else System.out.println("Class name: " + attributes.getValue(0)); } public void endElement(String uri, String localName, String qName) { System.out.println("End element: " + qName); inElement = false; } public void characters(char ch[], int start, int length) { if(inElement) System.out.println("Process : " + new String(ch, start, length)); } }
  17. 17. public void startElement(String uri, String localName, String qName, Attributes attributes) { if(qName != CLASSNAME){ element = qName; } else{ String className = attributes.getValue(0); System.out.println("Class name: " + className); object = ReflectionHelper.createIntance(className); } } public void endElement(String uri, String localName, String qName) { element = null; } public void characters(char ch[], int start, int length) { if(element != null){ String value = new String(ch, start, length); System.out.println(element + " = " + value); ReflectionHelper.setFieldValue(object, element, value); } }
  18. 18. javax.xml.parsers.DocumentBuilderFactory javax.xml.parsers.DocumentBuilder org.w3c.dom.Document File fXmlFile = new File(“test.xml”); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile);
  19. 19.     
  20. 20.
  21. 21. Вынести все параметры в XML файлы Создать class ResourceFactory (singleton) GameSessionResource resource = (GameSessionResource) ResourceFactory.instance().get(“./GameResource.xml”) Создать interface Resource и class GameSessionResource
  22. 22. 31. Сериализация/десериализация. Interface Serializable. 32. Ключевое слово transient. Причины использования transient. 33. Reflection. Роль reflection в сериализации. 34. Шаблон проектирования Factory method. 35. SAX парсер. 36. DOM парсер. 37. Ресурсная система сервера.

×