Skip to content

Instantly share code, notes, and snippets.

@Guaderxx
Created December 15, 2023 10:44
Show Gist options
  • Save Guaderxx/d4cf5296c52e1446bbc6b9c0e5bdb8ac to your computer and use it in GitHub Desktop.
Save Guaderxx/d4cf5296c52e1446bbc6b9c0e5bdb8ac to your computer and use it in GitHub Desktop.
Proxy pattern
package main
import "fmt"
type Driven interface {
Drive()
}
type Car struct{}
func (c *Car) Drive() {
fmt.Println("Car being driven")
}
type Driver struct {
Age int
}
type CarProxy struct {
car Car
driver *Driver
}
func (cp *CarProxy) Drive() {
if cp.driver.Age >= 16 {
cp.car.Drive()
} else {
fmt.Println("Driver too young")
}
}
func NewCarProxy(driver *Driver) *CarProxy {
return &CarProxy{Car{}, driver}
}
func main() {
car := NewCarProxy(&Driver{12})
car.Drive()
}
// output: Driver too yound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment