forked from go/golangs_learn
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.
27 lines
491 B
27 lines
491 B
package fly_weight
|
|
|
|
type FlyWeight struct {
|
|
Name string
|
|
}
|
|
|
|
func NewFlyWeight(name string) *FlyWeight {
|
|
return &FlyWeight{Name: name}
|
|
}
|
|
|
|
type FlyWeightFactory struct {
|
|
pool map[string]*FlyWeight
|
|
}
|
|
|
|
func NewFlyWeightFactory() *FlyWeightFactory {
|
|
return &FlyWeightFactory{pool: make(map[string]*FlyWeight)}
|
|
}
|
|
|
|
func (f *FlyWeightFactory) GetFlyWeight(name string) *FlyWeight {
|
|
weight, ok := f.pool[name]
|
|
|
|
if !ok {
|
|
weight := NewFlyWeight(name)
|
|
f.pool[name] = weight
|
|
}
|
|
return weight
|
|
}
|
|
|