著名的桥接模式罗。。
bridge.go
package bridge import ( "errors" "fmt" "io" ) type PrinterAPI interface { PrintMessage(string) error } type PrinterImpl1 struct{} func (p *PrinterImpl1) PrintMessage(msg string) error { fmt.Printf("%s\n", msg) return nil //return errors.New("Not implemented yet") } type PrinterImpl2 struct { Writer io.Writer } func (d *PrinterImpl2) PrintMessage(msg string) error { if d.Writer == nil { return errors.New("You need to pass an io.Writer to PrinterImpl2") } fmt.Fprintf(d.Writer, "%s", msg) return nil //return errors.New("Not implemented yet") } type TestWriter struct { Msg string } func (t *TestWriter) Write(p []byte) (n int, err error) { n = len(p) if n > 0 { t.Msg = string(p) return n, nil } err = errors.New("Content received on Writer was empty") return } type PrinterAbstraction interface { Print() error } type NormalPrinter struct { Msg string Printer PrinterAPI } func (c *NormalPrinter) Print() error { c.Printer.PrintMessage(c.Msg) return nil //return errors.New("Not implemented yet") } type PacktPrinter struct { Msg string Printer PrinterAPI } func (c *PacktPrinter) Print() error { c.Printer.PrintMessage(fmt.Sprintf("Message from Packt: %s", c.Msg)) return nil //return errors.New("Not implemented yet") }
bridge_test.go
package bridge import ( //"strings" "testing" ) func TestPrintAPI1(t *testing.T) { api1 := PrinterImpl1{} err := api1.PrintMessage("Hello") if err != nil { t.Errorf("Error trying to use the API1 implementation: Message: %s\n", err.Error()) } } func TestPrintAPI2(t *testing.T) { testWriter := TestWriter{} api2 := PrinterImpl2{ Writer: &testWriter, } expectedMessage := "Hello" err := api2.PrintMessage(expectedMessage) if err != nil { t.Errorf("Error trying to use the API2 implementation: %s\n", err.Error()) if testWriter.Msg != expectedMessage { t.Fatalf("API2 io.writer wrong.\n Actual: %s\nExpected: %s\n", testWriter.Msg, expectedMessage) } } } func TestNormalPrinter_Print(t *testing.T) { expectedMessage := "Hello io.Writer" normal := NormalPrinter{ Msg: expectedMessage, Printer: &PrinterImpl1{}, } err := normal.Print() if err != nil { t.Errorf(err.Error()) } testWriter := TestWriter{} normal = NormalPrinter{ Msg: expectedMessage, Printer: &PrinterImpl2{ Writer: &testWriter, }, } err = normal.Print() if err != nil { t.Errorf(err.Error()) } if testWriter.Msg != expectedMessage { t.Errorf("The expected message on the io.Writer is wrong.\n Actual: %s\nExpected: %s\n", testWriter.Msg, expectedMessage) } } func TestPacktPrinter_Print(t *testing.T) { passedMessage := "Hello io.Writer" expectedMessage := "Message from Packt: Hello io.Writer" packt := PacktPrinter{ Msg: passedMessage, Printer: &PrinterImpl1{}, } err := packt.Print() if err != nil { t.Errorf(err.Error()) } testWriter := TestWriter{} packt = PacktPrinter{ Msg: passedMessage, Printer: &PrinterImpl2{ Writer: &testWriter, }, } err = packt.Print() if err != nil { t.Errorf(err.Error()) } if testWriter.Msg != expectedMessage { t.Errorf("The expected message on the io.Writer is wrong.\n Actual: %s\nExpected: %s\n", testWriter.Msg, expectedMessage) } }