I ran into an a blog All Design Patterns in Go (Golang) but feel that it does not have to be this OOP and this complicated in Go. So I will try to re-implement these patterns in a more Go-ish way.
Behavioural Design Patterns
Observer pattern
Go has very good support for functional programming, making a lot of object-ish wrappers unnecessary.
Instead of having the observer
interface receiving a function, I can directly keep track of the functions to be invoked at events:
1 | type item struct { |
The functions to notify can be directly be registered to and invoked by the subject:
1 | func (i *item) register(id string) { |
Strategy pattern
While I’m here let me implement the caching strategies in full.
- Extended the
item
data type to keep track of theaccessCount
,lastAccessed
time andlastUpdated
time - Implemented the algos - although now not wrapped in an interface but passed in as a function.