Faced the choice of what to use: interfaces or displays. The idea is this: I have a dispatcher class, which must accept a class at the entrance, pull out information about it and serialize it into the network according to a certain protocol. Interested in what is better to use when extracting information about the class - the interface or display?
- oneo_0. Interestingly, I'm the only one who hears for the first time about the "display"? - kirelagin
- oneAaaaaa, "reflection"! - kirelagin
- Yes, yes .. I also thought at first that I must have missed something in my life :) - cy6erGn0m
3 answers
Use interfaces if possible. Reflections - very slowly.
UPD: It is difficult to explain, because in general it will be cumbersome through interfaces. It is necessary to proceed from the specifics of the transmitted data.
If the transmission format is not important to you (i.e. you do not need to follow any protocol), then just use the standard serialization
class MyData implements Serializable { private int myValue; // тут какой-то код.... } class StoreStreamAction implements Runnable { private final OutputStream os; private final MyData toBeWritten; // .... public void run() { try { ObjectOutputStream objs = new ObjectOutputStream(os); objs.writeObject(toBeWritten); } catch(IOException e) { //.... } } }
If you must follow the protocol, then it is very important to understand the specifics of the transmitted data and the features of the protocol itself. Otherwise, the example is very abstract.
interface Trasferrable { String getName(); void sendToStream(OutputStream os) throws IOException; } class MyData implements Transferrable { private int myValue; public String getName() { return "[my-data]"; } public void sendToStream(OutputStream os) throws IOException { os.write(myvalue & 0xff); os.write((myvalue >> 8) & 0xff); // .. etc } } class StoreStreamAction implements Runnable { private final OutputStream os; private final Transferrable toBeWritten; // .... public void run() { try { os.write(toBeWritten.getName().getBytes(Charset.forName("UTF-8"))); os.write(0); toBeWritten.sendToStream(os); } catch(IOException e) { //.... } } }
- With reflections it is clear how ... But with the interfaces it is not very ... please explain the idea in more detail. - Alex
unequivocally the interface, and I very much doubt the need to invent a new bike when everything has been thought out before you:
class MyData implements Serializable { private static final long serialVersionUID = 3294943541140409362L; private int myValue; private void readObject ( final java.io.ObjectInputStream in ) throws IOException, ClassNotFoundException { } private void readObjectNoData () throws ObjectStreamException { } private void writeObject ( final java.io.ObjectOutputStream out ) throws IOException { } }
You need to implement these methods that will be automatically used by the JVM during serialization (do you want to twist your bike? Use the proxy template and bring the implementation of these methods to another class, and there ... "though the grass does not grow")
We inherit a class that we want to transfer from some X interface (the interface, in turn, must be inherited from io.Serializable). The resulting class is checked for "instanceof" from this interface. If he already knows how to move it.