In android card game via bluetooth objects of several different types are transmitted. I do this with the help of objectOutputStream / objectInputStream in which the bluetooth stream is wrapped. How can I, together with the object itself, betray its type so that the recipient knows what type to bring the received object to? I see two ways:

1) Somehow to get an array of bytes, which will be a serialized object to be transferred. Add to the beginning of the array int, which will occupy a fixed number of bytes. Then the recipient will always be able to take a fixed number of bytes from the beginning of the array, lead to int and thus find out which type you need to cast the object that is written further in the received array.

2) Let the receiver always expect an int. Then I will be able to send an int to the first message (the recipient will go to the standby mode of a particular object), and the second to send the object itself.

I don’t really like both ways. How should I transfer objects?

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants katso , aleksandr barakin , Denis , user194374, Alex Nov 27 Nov '16 at 12:03 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    Very clever jars on from mentioning instanceof alone. And they put, and they put minuses, not bothering to explain themselves, to set Nubasa on the right path.

    You can solve your problem in the style of work-OOP using the template "visitor" (or "visitor" in Latin).

    We need a hierarchy of objects with a special method that accepts a visitor (also specially sharpened for this hierarchy).
    But since we are going to send something like int (or Integer, right?), Which is not expandable, let's agree to transfer our data in a special Message object (this is usually what everyone-carrier objects are called - messages, messages).
    For each class of data to be sent will be its own Message class.

     // глава херархии сообщений public abstract class Message<T> implements Serializable { // данные, которые передаются сообщением protected T data; public Message(T data) { this.data = data; } public T getData() { return data; } // Каждый конкретный класс должен переопределить этот метод по-своему public abstract void accept(MessageVisitor visitor); } public class IntegerMessage extends Message<Integer> { public IntegerMessage(Integer data) { super(data); } @Override public void accept(MessageVisitor visitor) { visitor.visit(this); } } public class StringMessage extends Message<String> { public StringMessage(String data) { super(data); } @Override public void accept(MessageVisitor visitor) { visitor.visit(this); } } ... 

    Now it may seem that they all override accept equally. But in fact it will be like that. We will define who is a visitor to MessageVisitor and, I hope, everything will become clear.

     public interface MessageVisitor { void visit(IntegerMessage msg); void visit(StringMessage msg); ... } 

    It turns out that for each class of the Message hierarchy, the visitor has his own overloaded method visit .
    So they are not so identical, these are our accept s. This is very important.

    Some basis is procured. Let's see how sending and receiving data may look like.

    Sending:

     Integer integer = ... String string = ... ... send(new IntegerMessage(integer)); send(new StringMessage(string)); 

    It's all clear. Wrap everything in a message of the appropriate class.

    The recipient must send the received messages to the visitor, who will extract and process the data according to the class of the message and data.

     Message msg = receive(); msg.accept(new MessageVisitor() { @Override void visit(IntegerMessage msg) { Integer data = msg.getData(); System.out.println("Integer: " + data); } @Override void visit(StringMessage msg) { String data = msg.getData(); System.out.println("String: " + data); } ... }); 

    The example uses an anonymous class for brevity, but nothing prevents you from using any class that implements MessageVisitor .

    msg.accept will call the overloaded visit for a valid msg class.
    In this simple way, the data gets into the necessary handler without any instanceof . And even casting is not needed = O
    There is now no reason for indiscretions to hate, now they are satisfied)

    Many bukav however. It would be better just clicked a minus.

    For the development of horizons can google about dispatching in the PLO.

    • Thank you so much! Understood, tonight rewrite the code. - Nikita Schmidt