Discussing the Observer pattern

Written by
Link to Post


Hey all, I want to preface this by saying that I’m still quite new to Go (only a few months in), and I wanted to have a discussion about a pattern I’ve used plenty before, the Observer pattern (also known as event/listener or pub/sub). I love this pattern because of how effective it is at decoupling code.

Recently I started up a new Go project and got to a point where I was implementing a document service which wrapped my repository data layer. I started thinking about adding search functionality, and immediately turned to the Observer pattern, because after all it’s not the document service’s responsibility to handle search or indexing. I banged out a super simple implementation which I’m happy with, but seeing it I started to question it. It felt a lot like something Go, and the Go community, had been guiding me away from; it felt like magic.

So I wanted to bring it to the sub and have a bit of a discussion on this pattern. It does a great job of decoupling code, but do you think the cost to human readability is not a worthy trade off? I’m starting to feel as though my code would be much easier to read and follow if I just leveraged composition instead of the Observer pattern. Let me know what you think!

submitted by /u/TBPixel
[link] [comments]

Article Tags:
· ·
Article Categories:

Leave a Reply