-
Notifications
You must be signed in to change notification settings - Fork 0
/
facotry.go
37 lines (31 loc) · 923 Bytes
/
facotry.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
/* Package statemachine
* @Author 砚池/Ivan
* @Date 2024/04/09
* @Description:
*/
package statemachine
import (
"fmt"
"github.com/samber/lo"
)
type Factory[S, E comparable, C any] struct {
container map[string]*StateMachine[S, E, C]
}
func (f *Factory[S, E, C]) Register(s *StateMachine[S, E, C]) error {
machineId := s.machineId
if f.container == nil {
f.container = make(map[string]*StateMachine[S, E, C])
}
if lo.Contains(lo.Keys(f.container), machineId) {
return fmt.Errorf("the state machine with id [%s] is already built, no need to build again\n", machineId)
}
f.container[machineId] = s
return nil
}
func (f *Factory[S, E, C]) Get(machineId string) (*StateMachine[S, E, C], error) {
if !lo.Contains(lo.Keys(f.container), machineId) {
return nil, fmt.Errorf("there is no stateMachine instance for %s, please build it first\n", machineId)
}
s := f.container[machineId]
return s, nil
}