Skip to content

Commit

Permalink
refactor: 重构factory注册
Browse files Browse the repository at this point in the history
  • Loading branch information
am6737 committed Mar 11, 2024
1 parent 1264795 commit 7def809
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
34 changes: 8 additions & 26 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"syscall"
)

//

var (
configFile string
)
Expand Down Expand Up @@ -48,30 +46,14 @@ func main() {

pushServiceFactory := factory.NewPushServiceFactory()
if err := pushServiceFactory.Register(
func() (push.PushService, error) {
return push.NewAPNsService(cfg, logger)
},
func() (push.PushService, error) {
return push.NewFCMService(cfg, logger)
},
func() (push.PushService, error) {
return push.NewHMSService(cfg, logger)
},
func() (push.PushService, error) {
return push.NewXiaomiService(cfg, logger)
},
func() (push.PushService, error) {
return push.NewVivoService(cfg, logger)
},
func() (push.PushService, error) {
return push.NewOppoService(cfg, logger)
},
func() (push.PushService, error) {
return push.NewMeizuService(cfg, logger)
},
func() (push.PushService, error) {
return push.NewHonorService(cfg, logger)
},
pushServiceFactory.WithPushService(push.NewAPNsService(cfg, logger)),
pushServiceFactory.WithPushService(push.NewFCMService(cfg, logger)),
pushServiceFactory.WithPushService(push.NewHMSService(cfg, logger)),
pushServiceFactory.WithPushService(push.NewXiaomiService(cfg, logger)),
pushServiceFactory.WithPushService(push.NewOppoService(cfg, logger)),
pushServiceFactory.WithPushService(push.NewVivoService(cfg, logger)),
pushServiceFactory.WithPushService(push.NewMeizuService(cfg, logger)),
pushServiceFactory.WithPushService(push.NewHonorService(cfg, logger)),
); err != nil {
panic(err)
}
Expand Down
8 changes: 2 additions & 6 deletions factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ func NewPushServiceFactory() *PushServiceFactory {
}
}

func WithPushServiceCreator(creator func() (push.PushService, error)) PushServiceCreator {
func (f *PushServiceFactory) WithPushService(ps push.PushService, err error) PushServiceCreator {
return func() (push.PushService, error) {
ps, err := creator()
if err != nil {
return nil, err
}
return ps, nil
return ps, err
}
}

Expand Down

0 comments on commit 7def809

Please sign in to comment.