苏州瑞玛APS项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.2 KiB

  1. // Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
  2. package container
  3. import (
  4. "errors"
  5. "fmt"
  6. )
  7. // 组件句柄管理器
  8. type HandlerManager struct {
  9. // 组件句柄映射: map[接口类型]组件句柄
  10. handlerMapping map[Interface]ComponentHandler
  11. }
  12. // 创建组件句柄管理器
  13. // 参数
  14. // 1.会话标识
  15. // 2.用户信息
  16. // 3.服务映射: map[接口类型]组件句柄
  17. // 返回值:
  18. // 1.会话
  19. // 2.错误
  20. func NewHandlerManager(handlerMapping map[Interface]ComponentHandler) (*HandlerManager, error) {
  21. if len(handlerMapping) > 0 {
  22. for interfaceType, handler := range handlerMapping {
  23. if interfaceType == nil {
  24. return nil, errors.New(fmt.Sprintf("组件接口不能为空!"))
  25. }
  26. if handler == nil {
  27. return nil, errors.New(fmt.Sprintf("组件句柄不能为空!"))
  28. }
  29. }
  30. }
  31. return &HandlerManager{handlerMapping}, nil
  32. }
  33. // 获取指定接口的组件句柄
  34. // 参数
  35. // 1.服务接口类型
  36. // 返回值:
  37. // 1.组件句柄
  38. func (session *HandlerManager) Handler(interfaceType Interface) ComponentHandler {
  39. if interfaceType == nil {
  40. return nil
  41. }
  42. componentHandler, ok := session.handlerMapping[interfaceType]
  43. if !ok {
  44. return nil
  45. }
  46. return componentHandler
  47. }