代理模式,单元测试用例真的写得详细,
受教~
proxy.go
package proxy
import (
//"errors"
"fmt"
)
type UserFinder interface {
FindUser(id int32) (User, error)
}
type User struct {
ID int32
}
type UserList []User
func (t *UserList) FindUser(id int32) (User, error) {
for i := 0; i < len(*t); i++ {
if (*t)[i].ID == id {
return (*t)[i], nil
}
}
return User{}, fmt.Errorf("User %d could not be found\n", id)
}
type UserListProxy struct {
SomeDatabase UserList
StackCache UserList
StackCapacity int
DidLastSearchUsedCache bool
}
func (u *UserListProxy) FindUser(id int32) (User, error) {
user, err := u.StackCache.FindUser(id)
if err == nil {
fmt.Println("Returning user from cache")
u.DidLastSearchUsedCache = true
return user, nil
} else {
user, err = u.SomeDatabase.FindUser(id)
if err != nil {
return User{}, err
}
fmt.Println("Returning from database")
u.addUserToStack(user)
u.DidLastSearchUsedCache = false
return user, nil
}
}
func (t *UserList) addUser(newUser User) {
*t = append(*t, newUser)
}
func (u *UserListProxy) addUserToStack(user User) {
if len(u.StackCache) >= u.StackCapacity {
u.StackCache = append(u.StackCache[1:], user)
} else {
u.StackCache.addUser(user)
}
}
packag