官术网_书友最值得收藏!

The mediator pattern

In many cases, when we design and develop software applications, we encounter many scenarios where we have modules and objects that have to communicate with one another. The easiest approach would be to make them in such a way that they know each other and can send messages directly.

However, this might create a mess. If we imagine, for example, a communication app in which each client has to connect to another one, it doesn't make sense for a client to manage many connections. A better solution would be to connect to a central server and for the server to manage the communication between the clients. The client sends the message to the server and the server keeps the connection active to all the clients, and it can broadcast messages to all required recipients.

Another example is where a specialized class is required to mediate between different controls, such as buttons, dropdowns, and list controls, in a graphical interface. For example, the graphical controls in a GUI can hold a reference to each other in order to invoke their methods reciprocally. But obviously, this will create an extremely coupled code in which each control depends on all the others. A better approach would be to make the parent responsible for broadcasting messages to all the required controls when something needs to be done. When something modifies in a control, it will notify the window, which will check which controls need to be informed and then inform them.

主站蜘蛛池模板: 麻江县| 福泉市| 仁布县| 虞城县| 长顺县| 阿拉尔市| 马鞍山市| 哈尔滨市| 永春县| 普兰店市| 读书| 昌吉市| 白水县| 广德县| 黔东| 峡江县| 瑞安市| 吉安县| 安多县| 太仓市| 南华县| 子洲县| 双鸭山市| 甘洛县| 阿荣旗| 承德市| 诸城市| 中宁县| 土默特左旗| 宝应县| 绥江县| 交口县| 虞城县| 永川市| 博爱县| 阿拉尔市| 永宁县| 锡林浩特市| 徐汇区| 焉耆| 五台县|