How is the publish-subscribe pattern different from gotos?

My understanding is that Goto statements are generally frowned upon. But the publish-subscribe pattern seems to be conceptually similar in that when a piece of code publishes a message, it performs a one-way transfer of control. The programmer may have no idea what parts of the program are subscribing to this message.

I have see something similar in a lot of JavaScript programs in which events are used to conveniently “hop” across modules. Am I missing something about the publish-subscribe or event-driven patterns?

5

Yep, you are definitely missing something. Gotos would typically be used, like you said, to perform a one-way transfer of control.

However, events do not do that. When the code fires the event, it knows full well that once event is published (or processed, queued, fired… etc) code execution will resume on the very next line in the code that generated the event.

The use of goto creates a very tight coupling between the code that calls that statement and the code that’s on the receiving end. Developer has to have intimate knowledge of both places in order to use goto.

On the other hand, code that fires events would typically not know or care who is interested in listening for that event. There could be a listener. Or there could be a 100 listeners or 0. Those listeners could be in the same program where event was fired, or they could be in a completely different application, or they could be on a different machine. As far as the publisher is concerned, as soon as he generates the event his job is done.

If you are with me so far, what I described above is the ideal case of pub/sub pattern. Unfortunately in the real world things aren’t always ideal and there are cases where publishers generates an event, a subscriber gets invoked, changes a whole bunch of state and by the time code execution returns back to the publisher “the world” appears to have been turned upside down. And I’m sure you’ve run into this in the past, because this condition often arises when pub/sub pattern is implemented in a very simple fashion (e.g. via use of delegates or events in C#, or function/interface pointers in C/C++).

But this problem isn’t necessarily pub/sub pattern but rather the implementation of it. This is why many systems rely on queues so that when an event is published, it is simply queued up to be invoked later giving the publisher a chance to finish execution while the world is still intact. When publisher is done doing it’s work, an event loop (aka dispatch loop) will pop off the events and invoke subscribers.

1

There are a couple of differences. First, when a code executes GOTO, it gives up control, and there’s no guarantee that it will regain control. A publisher in pub/sub, however, will keep running and performing its logic, sending off messages as appropriate. Its behavior is understandable and predicable.

Secondly, the subscriber will receive messages, and unlike with GOTO, the message itself carries context. Both the type of message, and any properties it carries, help tell the subscriber perform its role. And, after handling a message, the subscriber is still able to take new messages. So it’s behavior too is understandable and predictable.

The big difference is that the publisher and subscriber have a well-defined flow of execution, and they will, in essence, keep looping and doing their jobs, while sending and receiving messages. Code with GOTOs can be well-written and orderly, but it can also degrade, and there isn’t the same guarantee of clearly understood behavior.

You’re right, though. Someone could write a pub/sub system with so many messages and so many little jumps that keeping track of the processing flow could become a nightmare. And on the other hand, you could write a system with GOTOs that behaves extremely orderly and is easily understood. (I’m thinking of assembly code for very complex systems before symbolic languages took over.)

But typically, the decoupling you gain from pub/sub simplifies the problem of distributed processing and decouples logic within your system. Also typically, straight-up GOTOs tend to create complicated systems where understanding the flow of control becomes problematic.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How is the publish-subscribe pattern different from gotos?

My understanding is that Goto statements are generally frowned upon. But the publish-subscribe pattern seems to be conceptually similar in that when a piece of code publishes a message, it performs a one-way transfer of control. The programmer may have no idea what parts of the program are subscribing to this message.

I have see something similar in a lot of JavaScript programs in which events are used to conveniently “hop” across modules. Am I missing something about the publish-subscribe or event-driven patterns?

5

Yep, you are definitely missing something. Gotos would typically be used, like you said, to perform a one-way transfer of control.

However, events do not do that. When the code fires the event, it knows full well that once event is published (or processed, queued, fired… etc) code execution will resume on the very next line in the code that generated the event.

The use of goto creates a very tight coupling between the code that calls that statement and the code that’s on the receiving end. Developer has to have intimate knowledge of both places in order to use goto.

On the other hand, code that fires events would typically not know or care who is interested in listening for that event. There could be a listener. Or there could be a 100 listeners or 0. Those listeners could be in the same program where event was fired, or they could be in a completely different application, or they could be on a different machine. As far as the publisher is concerned, as soon as he generates the event his job is done.

If you are with me so far, what I described above is the ideal case of pub/sub pattern. Unfortunately in the real world things aren’t always ideal and there are cases where publishers generates an event, a subscriber gets invoked, changes a whole bunch of state and by the time code execution returns back to the publisher “the world” appears to have been turned upside down. And I’m sure you’ve run into this in the past, because this condition often arises when pub/sub pattern is implemented in a very simple fashion (e.g. via use of delegates or events in C#, or function/interface pointers in C/C++).

But this problem isn’t necessarily pub/sub pattern but rather the implementation of it. This is why many systems rely on queues so that when an event is published, it is simply queued up to be invoked later giving the publisher a chance to finish execution while the world is still intact. When publisher is done doing it’s work, an event loop (aka dispatch loop) will pop off the events and invoke subscribers.

1

There are a couple of differences. First, when a code executes GOTO, it gives up control, and there’s no guarantee that it will regain control. A publisher in pub/sub, however, will keep running and performing its logic, sending off messages as appropriate. Its behavior is understandable and predicable.

Secondly, the subscriber will receive messages, and unlike with GOTO, the message itself carries context. Both the type of message, and any properties it carries, help tell the subscriber perform its role. And, after handling a message, the subscriber is still able to take new messages. So it’s behavior too is understandable and predictable.

The big difference is that the publisher and subscriber have a well-defined flow of execution, and they will, in essence, keep looping and doing their jobs, while sending and receiving messages. Code with GOTOs can be well-written and orderly, but it can also degrade, and there isn’t the same guarantee of clearly understood behavior.

You’re right, though. Someone could write a pub/sub system with so many messages and so many little jumps that keeping track of the processing flow could become a nightmare. And on the other hand, you could write a system with GOTOs that behaves extremely orderly and is easily understood. (I’m thinking of assembly code for very complex systems before symbolic languages took over.)

But typically, the decoupling you gain from pub/sub simplifies the problem of distributed processing and decouples logic within your system. Also typically, straight-up GOTOs tend to create complicated systems where understanding the flow of control becomes problematic.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật