Skip to main content

Go Example

package main

type printer interface {
printFile()
}
package main

import "fmt"

type epson struct {
}

func (p *epson) printFile() {
fmt.Println("Printing by a EPSON Printer")
}
package main

import "fmt"

type hp struct {
}

func (p *hp) printFile() {
fmt.Println("Printing by a HP Printer")
}
package main

type computer interface {
print()
setPrinter(printer)
}
package main

import "fmt"

type mac struct {
printer printer
}

func (m *mac) print() {
fmt.Println("Print request for mac")
m.printer.printFile()
}

func (m *mac) setPrinter(p printer) {
m.printer = p
}
package main

import "fmt"

type windows struct {
printer printer
}

func (w *windows) print() {
fmt.Println("Print request for windows")
w.printer.printFile()
}

func (w *windows) setPrinter(p printer) {
w.printer = p
}
package main

import "fmt"

func main() {

hpPrinter := &hp{}
epsonPrinter := &epson{}

macComputer := &mac{}

macComputer.setPrinter(hpPrinter)
macComputer.print()
fmt.Println()

macComputer.setPrinter(epsonPrinter)
macComputer.print()
fmt.Println()

winComputer := &windows{}

winComputer.setPrinter(hpPrinter)
winComputer.print()
fmt.Println()

winComputer.setPrinter(epsonPrinter)
winComputer.print()
fmt.Println()
}
Print request for mac
Printing by a HP Printer

Print request for mac
Printing by a EPSON Printer

Print request for windows
Printing by a HP Printer

Print request for windows