Suitable Design Pattern for a Simple Chat Application

后端 未结 3 1741
感情败类
感情败类 2020-12-25 08:26

I am designing a simple chat application (just for the kick of it). I have been wondering of a simple design for that chat application. To give you overview.. here are the r

3条回答
  •  囚心锁ツ
    2020-12-25 09:15

    I would suggest looking into messaging frameworks instead of using Observer pattern.

    Take a look at this simple implementation which will be sufficient for your toy project - eventbus (no longer available). Alternatively you could go with full blown JMS implementation like ActiveMQ.

    Basically it allows you to define a common bus to which you can register and unregister participants, and also send messages that all of the participants will see. The big advantage over observer pattern is the very low coupling between the participants - you don't register with each object to get his messages - you just register once with the bus. In addition you get asynchronous processing - let's say you have 1000 chat sessions - if you use observer it means that for each message to complete it will take to update 1000 sessions. With message framework message send is very quick, and notifying all 1000 sessions is done in the background.

提交回复
热议问题