设计模式 - 拦截过滤器模式
设计模式 - 传输对象模式

设计模式 - 服务定位器模式

violet posted @ 5 年前 in 笔记 with tags Design Pattern Golang , 469 阅读

服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。

  • 服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
  • Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。
  • 服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
  • 缓存(Cache) - 缓存存储服务的引用,以便复用它们。
  • 客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。

 

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
 
import "fmt"
 
type Service interface {
    GetName() string
    Execute()
}
 
type Service1 struct{}
 
func (s *Service1) Execute() {
    fmt.Println("Executing service1")
}
 
func (s *Service1) GetName() string {
    return "Service1"
}
 
type Service2 struct{}
 
func (s *Service2) Execute() {
    fmt.Println("Executing service2")
}
 
func (s *Service2) GetName() string {
    return "Service2"
}
 
type InitContext struct{}
 
func (i *InitContext) Lookup(jndiName string) Service {
    if jndiName == "service1" {
        fmt.Println("looking up and creating a new service1 object")
        return &Service1{}
    }
    if jndiName == "service2" {
        fmt.Println("looking up and creating a new service2 object")
        return &Service2{}
    }
    return nil
}
 
type Cache struct {
    Services map[string]Service
}
 
func NewCache() *Cache {
    return &Cache{
        Services: map[string]Service{},
    }
}
 
func (c *Cache) GetService(serviceName string) Service {
    if v, ok := c.Services[serviceName]; ok {
        fmt.Println("return cached service: ", serviceName)
        return v
    }
    return nil
}
 
func (c *Cache) AddService(name string, service Service) {
    c.Services[name] = service
}
 
type ServiceLocator struct {
    Cache *Cache
}
 
func NewServiceLocator() *ServiceLocator {
    return &ServiceLocator{
        Cache: NewCache(),
    }
}
 
func (s *ServiceLocator) GetService(jndiName string) Service {
    service := s.Cache.GetService(jndiName)
    if service != nil {
        return service
    }
    // create a new service1
    context := &InitContext{}
    service = context.Lookup(jndiName)
    s.Cache.AddService(jndiName, service)
    return service
}
 
func main() {
    locator := NewServiceLocator()
    service := locator.GetService("service1")
    service.Execute()
 
    service = locator.GetService("service2")
    service.Execute()
 
    service = locator.GetService("service1")
    service.Execute()
 
    service = locator.GetService("service2")
    service.Execute()
}
Delhi High Class Esc 说:
3 年前

It is very great and it is very nice to achieve the best fun with the available time.

Escorts in Delhi 说:
3 年前

I am happy to be here and this wonderful blog. I have found here lots of important information for my knowledge i need.
http://www.tanvirai.in/

Hyderabad Call Girls 说:
2 年前

There are loads of overall quite extraordinary equity through which we can have a bunches of incredible minutes.

Raipur Escorts 说:
2 年前

A responsibility of appreciation is for the epic data and pieces of data you have given here.

Lucknow Escorts Serv 说:
2 年前

Appreciation for the unfathomable post you posted. I like how you depict the momentous substance. The focuses you raise are attested and sensible.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter