-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathfacade.go
59 lines (48 loc) · 1.09 KB
/
facade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package facade
// IUser 用户接口
type IUser interface {
Login(phone int, code int) (*User, error)
Register(phone int, code int) (*User, error)
}
// IUserFacade 门面模式
type IUserFacade interface {
LoginOrRegister(phone int, code int) (*User, error)
}
// User 用户
type User struct {
Name string
}
func newUser(name string) IUser {
return User{Name: name}
}
// Login 登录
func (u User) Login(phone int, code int) (*User, error) {
// 校验操作 ...
return &User{Name: "test login"}, nil
}
// Register 注册
func (u User) Register(phone int, code int) (*User, error) {
// 校验操作 ...
// 创建用户
return &User{Name: "test register"}, nil
}
// UserService facade struct
type UserService struct {
User IUser
}
func NewUserService(name string) UserService {
return UserService{
User: newUser(name),
}
}
// LoginOrRegister 登录或注册
func (u UserService) LoginOrRegister(phone int, code int) (*User, error) {
user, err := u.User.Login(phone, code)
if err != nil {
return nil, err
}
if user != nil {
return user, nil
}
return u.User.Register(phone, code)
}