Created
December 15, 2023 10:44
-
-
Save Guaderxx/d4cf5296c52e1446bbc6b9c0e5bdb8ac to your computer and use it in GitHub Desktop.
Proxy pattern
This file contains hidden or 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 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