Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Last active May 31, 2016 12:30
Show Gist options
  • Save jochasinga/d9bc3f088cdf43c8955054164a036b15 to your computer and use it in GitHub Desktop.
Save jochasinga/d9bc3f088cdf43c8955054164a036b15 to your computer and use it in GitHub Desktop.
Sample structs which violate Dependency Inversion Principle
package main
import "log"
// This violates Dependency Inversion Principle
type LogWriter struct {
Counter int
}
func (w *LogWriter) Write(msg string) {
log.Println(msg)
w.Counter++
}
// Watcher tightly couples with LogWriter
// with its instance as a struct field.
type Watcher struct {
Writer *LogWriter
}
// Then this method calls the LogWriter's method
func (w *Watcher) Notify(msg string) {
w.Writer.Write(msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment