title: Java client-server chat protocol design
body: <p>I need to create a protocol for client-server chat on Java.
  I use xml to encode messages between them, but I can't decide how to return decoded data to server.</p>
  
  <p>First time, i created many object <code>implements IAction</code> (e.g. <code>Auth</code> object represents a packet, about new user in chat) which vary depending on their content.
  So, when server receives a socket, it calls my <code>public IAction decode(String s)</code> and then, server should determine what object it is:</p>
  
  <pre><code>if (myIAction instanceof Auth) {
     server.doMagicAuth((Auth) myIAction);
  }
  </code></pre>
  
  <p>but it was looking ugly to me coz of typecasting and many ifes</p>
  
  <p>Another approach was to create one common object, called <code>Packet</code>, that can contain <code>intValue</code>, <code>floatValue</code>, <code>stringValue</code> etc. And also it had a flag, that talked about what kind of packet it is.
  But it was the worst idea coz of collisions. For example, how to send 2 int values it this packet? e.g. password and users_id?</p>
  
  <p>And the latest idea was to observe server.
  Server should have many overloaded <code>public void action(IAction a)</code> methods (<code>action(Auth a)</code> ... etc), and protocol should notify a server depending on what kind of packet it is.</p>
  
  <p>And i cant decide, what method to use. Maybe you have some completely different ideas?</p>