Skip to content

Instantly share code, notes, and snippets.

@darox
Last active December 19, 2023 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darox/5cd795794c8a72c5d28dfaa1ccc818b9 to your computer and use it in GitHub Desktop.
Save darox/5cd795794c8a72c5d28dfaa1ccc818b9 to your computer and use it in GitHub Desktop.
Golang DPI
package main
import "fmt"
type Cleaner struct {
cleaningMethod string
tools []string
}
type CoffeeMachine struct {
Cleaner Cleaner
}
type Cleanable interface {
clean()
setTools(tools []string)
}
func (c *Cleaner) setTools(tools []string) {
c.tools = tools
}
func (c *Cleaner) clean() {
fmt.Printf("%s cleaning with the following tools: \n", c.cleaningMethod)
for _, tool := range c.tools {
fmt.Println(tool)
}
fmt.Println()
}
func NewCoffeeMachine(cleaner *Cleaner) *CoffeeMachine {
return &CoffeeMachine{
Cleaner: *cleaner,
}
}
func main() {
// Init the wet cleaner
wetCleaner := &Cleaner{
cleaningMethod: "wet",
}
// Set the tools that are needed for the wet cleaning
wetCleaner.setTools([]string{"water", "soap"})
// Init the coffee machine with the wet cleaner
coffeeMachine := NewCoffeeMachine(wetCleaner)
// Clean the coffee machine
coffeeMachine.Cleaner.clean()
// Add a new tool to the wet cleaner
coffeeMachine.Cleaner.setTools([]string{"water", "soap", "sponge"})
// Clean the coffee machine again with the wet cleaner and the new tools
coffeeMachine.Cleaner.clean()
// Init the dry cleaner
dryCleaner := Cleaner{
cleaningMethod: "dry",
}
// change the implementation of the cleaner
coffeeMachine.Cleaner = dryCleaner
// Set the tools that are needed for the dry cleaning
coffeeMachine.Cleaner.setTools([]string{"cloth", "brush"})
// Clean the coffee machine with the dry cleaner
coffeeMachine.Cleaner.clean()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment