Skip to content

Instantly share code, notes, and snippets.

package main
type decreaseVolumeCommand struct {
device device
}
func (c *decreaseVolumeCommand) execute() {
c.device.decreaseVolume()
}
@Israel-Miles
Israel-Miles / tv.go
Created November 11, 2020 20:51
command-pattern
package main
import "fmt"
type tv struct {
isOn bool
volume int
}
func (t *tv) on() {
@Israel-Miles
Israel-Miles / button.go
Created November 11, 2020 20:54
command-pattern
package main
type button struct {
command command
}
func (b *button) press() {
b.command.execute()
}
@Israel-Miles
Israel-Miles / main.go
Last active November 11, 2020 21:00
command-pattern
package main
func main() {
tv := &tv {
isOn: false,
volume: 10,
}
// Instantiate commands
onCommand := &onCommand{
@Israel-Miles
Israel-Miles / observer.go
Created November 18, 2020 18:05
observer pattern
package main
import "fmt"
type Observer interface {
Update(value int)
}
func createWeatherStation(name string) *WeatherStation{
return &WeatherStation{Name:name}
@Israel-Miles
Israel-Miles / subject.go
Last active November 18, 2020 18:15
observer pattern
package main
import (
"fmt"
"math/rand"
)
const (
maxTemperature = 120
minTemperature = -30
@Israel-Miles
Israel-Miles / main.go
Last active November 18, 2020 18:38
observer pattern
package main
func main(){
// Initialise our sensors and stations
coSensor := createWeatherSensor()
wySensor:= createWeatherSensor()
denverStation := createWeatherStation("Denver Weather Station")
vailStation := createWeatherStation("Vail Weather Station")
cheyenneStation := createWeatherStation("Cheyenne Weather Station")
@Israel-Miles
Israel-Miles / pizza.go
Last active November 19, 2020 23:45
decorator pattern
package main
type pizza interface {
getPrice() float64
getCalories() int
}
type cheesePizza struct {
calories int
price float64
@Israel-Miles
Israel-Miles / toppings.go
Last active November 20, 2020 01:19
command pattern
package main
const (
pepperoniPrice = 1.75
artichokePrice = 2.00
anchoviePrice = 2.50
tomatoePrice = 0.75
pepperoniCalories = 200
artichokeCalories = 150
@Israel-Miles
Israel-Miles / main.go
Last active November 19, 2020 23:55
command pattern
package main
import "fmt"
func main() {
theBombPizza := &anchovieTopping{
pizza: &artichokeTopping{
pizza: &tomatoeTopping{
pizza: newCheesePizza(),