Using a collection of Chats Vs a single Chat object

Hello,

I’'m developing a communication solution that uses XMPP, so that I have the following situation:

each object in my application can communicate to many other objects, using XMPP, through Chat instances. By the way, the application is monothreaded. Hence, it is not possíble to send two or more messages in a concurrent way. Considering that each Chat to some recipient uses the same connection, my question is:

  • Should I have a collection of Chat objects, retrieving the right one in order to talk to the right recipient? or it would be better to have a single Chat instance that can be created again (if necessary) to talk to another recipient?

the first solution requires a greater use of memory, and is more difficult to manage the destruction of Chat instances. In the second solution, I have to take care about setting the “FilteredOnThreadID” to false. If I choose the second solution, is there a great overhead on creating again the Chat instances each time I need to talk to a different recipient?

thanks in advance,

Aliandro

Aliandro,

Conceptually, you would be better off creating a new chat instance for each connection to different objects. The most elegant thing would probably be including an array of chats as a private data member of your ‘‘CommunicationObject’’. This way, he has individual chats that manage routing of messages to the right object and also let the receiving object know who sent the message.

You certainly shouldn’‘t worry about memory usage…unless your writing a server side application where thousands of these will potentially be created. If that is[/i] what you are doing, you’'ll probably want to create a manager type class that maintains one chat connection to each object, and has utility methods that will route messages to the appropriate object. That is, if A wants to send a message to B, he calls ChatManager.sendMessage(B, yourMessage).

Let me know if these ideas are helpful, or just muddy the water!

-Ken