Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Last active February 9, 2017 07:17
Show Gist options
  • Save deadprogram/f5368de58f33a4022b99efbc77fb0c9c to your computer and use it in GitHub Desktop.
Save deadprogram/f5368de58f33a4022b99efbc77fb0c9c to your computer and use it in GitHub Desktop.
package main
import "fmt"
type tableServer struct {
table int
}
type TableServer interface {
SetTable(table int)
GetTable() int
}
func NewTableServer() TableServer {
return &tableServer{}
}
func (i *tableServer) SetTable(table int) {
i.table = table
}
func (i *tableServer) GetTable() int {
return i.table
}
func SetTable(table int) func(TableServer) {
return func(i TableServer) {
i.SetTable(table)
}
}
type Waiter struct {
Name string
TableServer
}
func NewWaiter(name string, options ...func(TableServer)) *Waiter {
w := &Waiter{
Name: name,
TableServer: NewTableServer(),
}
for _, option := range options {
option(w)
}
return w
}
type Host struct {
Name string
TableServer
}
func NewHost(name string, options ...func(TableServer)) *Host {
w := &Host{
Name: name,
TableServer: NewTableServer(),
}
for _, option := range options {
option(w)
}
return w
}
func main() {
waiter := NewWaiter("joe")
fmt.Println(waiter.Name, "is working table", waiter.GetTable())
waiter = NewWaiter("bob", SetTable(9))
fmt.Println(waiter.Name, "is working table", waiter.GetTable())
host := NewHost("tom", SetTable(3))
fmt.Println(host.Name, "is working table", host.GetTable())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment