聊天应用的中介模式或观察者模式? [英] Mediator Pattern or Observer Pattern for Chat Application ?

查看:76
本文介绍了聊天应用的中介模式或观察者模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Skype-Clone聊天应用程序,并且想要使用Mediator Pattern或Observer Pattern。



但是,这两种模式有什么不同?经过几个小时的搜索和阅读,仍然令人困惑,所以:



1. Mediator和Observer之间的主要区别是什么?



2.使用聊天应用程序,哪一个是最佳选择?



3.如果我的话问题是错的,请给我一些建议现实世界如何处理聊天应用(架构师,模式......)。



P / s:我申请的主要想法是:

- 用户可以查看谁在线

- 用户可以在单独的窗口中与另一个用户进行通信(聊天,发送文件...)

- 如果超过2个用户在1个聊天室中,当1个用户发送数据时(文本,文件...)到服务器,服务器将广播这些数据给房间里的另一个用户(发件人除外)



我知道如何在客户之间沟通 ,但真实世界是如何做到的?请帮助我,并感谢您的阅读。

I developing a Skype-Clone chat application, and wanna use Mediator Pattern or Observer Pattern.

But, what is the different between those 2 patterns ? After hours of searching and reading, still confusing about it, so :

1. What is the main different between Mediator and Observer ?

2. With a chat application, which one is the best choice ?

3. If my question is wrong, please give me some advises how the real-world do with chat applicattion (architect, pattern...).

P/s: The main idea of my application is :
- User can view who is online
- User can communicate (chatting, sending files...) with another Users in seperate window
- If more than 2 Users in 1 "Chat-Room", when 1 User send data (text, file...) to Server, Server will "broadcast" those data to another Users in the Room (except sender)

I know how to "communicate" between Clients, but how the real-world do it ? Please help me, and thanks for reading.

推荐答案

中介模式提供两个服务之间的接口,以便它们可以独立开发 - 如果您依赖于服务由其他人开发(例如API)然后使用中介模式是有意义的。



观察者模式是一种状态变化的方式需要通知的事物(观察者)列表。



这些模式不是互斥的 - 你可以,例如使用观察者模式,让你的通知通过调解员,他们需要发送到外部api 。如果你想使用短信服务提供商提供非应用信息,我肯定会推荐这个。



作为一般规则,存在解决问题的模式,所以最好的办法是你可以解决你可能遇到的问题类型,然后选择适合解决它们的模式。将模式视为架构不是一个好主意(imo)。
The mediator pattern provides an interface between two services so that they can develop independently - if you are depending on a service that is developed by someone else (such as an API) then using a mediator pattern makes sense.

The observer pattern is a way of state changes in one thing being notified to a list of things (observers) that need to be notified.

These patterns are not mutually exclusive - you could, for example use the observer pattern and have your notifications go through a mediator where they need to be sent to an external api. I would definitely recommend this if you want to use an SMS provider for off-app messages.

As a general rule, patterns exist to solve problems so the best thing to do is work out the types of problem you are likely to encounter and then pick the pattern(s) appropriate to solving them. Treating a pattern as an architecture is not a good idea (imo).


这篇关于聊天应用的中介模式或观察者模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆