Last active
December 19, 2023 13:01
-
-
Save darox/5cd795794c8a72c5d28dfaa1ccc818b9 to your computer and use it in GitHub Desktop.
Golang DPI
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 "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