简体   繁体   中英

How does publish-subscribe design pattern establish loose coupling between components? C#

If was going through a C# exam book and I read:

A popular design pattern (a reusable solution for a recurring problem) in application development is that of publish-subscribe. You can subscribe to an event and then you are notified when the publisher of the event raises a new event. This is used to establish loose coupling between components in an application.

It got me wondering that what does this have to do with loose coupling? Is it only because the components are now not interacting directly or do we have more reasons?

There are 2 important points in this.

1) user don't need to wait for the result or fire a event like click or refresh to get the changes this decouples the event source form front end to back end

2) I am not sure you will do this but event sourcing is broadly used in publish subscriptions mechanism and this will allow you decople domain entity alot

Ex. Employ salary credited will also trigger an event for creating a pay slip which will add the pdf on pay slip page of employee and send mail without employees salary This will decouple employee from pay slip generate service, and mailing also. And also update the UI for end user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM