Last active
May 31, 2016 12:30
-
-
Save jochasinga/d9bc3f088cdf43c8955054164a036b15 to your computer and use it in GitHub Desktop.
Sample structs which violate Dependency Inversion Principle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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