对象池模式

  1. Object Pool Pattern
  2. 实现
    1. 对象池定义
    2. 使用
  3. 注意

Object Pool Pattern

  对象池模式是一种创建型模式,根据需求来预测将要使用的对象,提前创建并保存在内存中。

实现

对象池定义

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
package pool

import (
"fmt"
"strconv"
)

type Object struct{

}

func (Object)Do(index int){
fmt.Println("Object Do:"+strconv.Itoa(index))
}


type Pool chan *Object

func NewPool(total int)*Pool{
p := make(Pool,total)
for i := 0;i<total;i++{
p <- new(Object)
}
return &p
}


使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func main(){
p := pool.NewPool(5)
wait := sync.WaitGroup{}
for i:=0;i<100;i++ {
index := i
wait.Add(1)
go func(pool pool.Pool, ind int) {
select {
case Obj := <-pool:
Obj.Do(ind)
pool <- Obj
default:
fmt.Println("No Object:"+strconv.Itoa(ind))
}
wait.Done()
}(*p, index)
}
wait.Wait()
}


  这里使用goroutines来并发地读取pool中的对象.

注意

  • 当创建对象的代价比维护代价更高的时候,使用对象池模式是极好的。
  • 如果需求相对固定,那么维护对象的代价可能得不偿失
  • 提前初始化对象对性能有积极的影响
Author:寒江雪
Date:2018 03 10

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件