flyweight pattern

This commit is contained in:
nynicg 2019-05-05 23:12:47 +08:00
parent 280a0ab36a
commit 178e5eff4c
2 changed files with 48 additions and 1 deletions

View File

@ -29,7 +29,7 @@ A curated collection of idiomatic design & application patterns for Go language.
| [Composite](/structural/composite/main.go) | Encapsulates and provides access to a number of different objects | ✔ | | [Composite](/structural/composite/main.go) | Encapsulates and provides access to a number of different objects | ✔ |
| [Decorator](/structural/decorator.md) | Adds behavior to an object, statically or dynamically | ✔ | | [Decorator](/structural/decorator.md) | Adds behavior to an object, statically or dynamically | ✔ |
| [Facade](/structural/facade/main.go) | Uses one type as an API to a number of others | ✔ | | [Facade](/structural/facade/main.go) | Uses one type as an API to a number of others | ✔ |
| [Flyweight](/structural/flyweight.md) | Reuses existing instances of objects with similar/identical state to minimize resource usage | | | [Flyweight](/structural/flyweight/main.go) | Reuses existing instances of objects with similar/identical state to minimize resource usage | |
| [Proxy](/structural/proxy.md) | Provides a surrogate for an object to control it's actions | ✔ | | [Proxy](/structural/proxy.md) | Provides a surrogate for an object to control it's actions | ✔ |
## Behavioral Patterns ## Behavioral Patterns

View File

@ -0,0 +1,47 @@
// 享元模式 flyweight pattern.
// 通过保存不同特征的实例以达到复用的效果,从而节省内存和优化性能.
// 与对象池模式的不同之处在于享元模式所保存的实例具有不同的特征,而对象池则全部是相同的实例.
// 这里以蛋糕为例字段flavour为其味道根据不同的制作者将不同的实例保存起来.
// 当实例的特征较少时,享元模式还可以和单例模式相结合.
package main
type Cake struct {
Flavour string
}
type CakeFactory struct {
Cakes map[string]Cake
}
func (f CakeFactory)NewCake(flavour string)Cake{
if c ,ok := f.Cakes[flavour];ok{
println("get an existing" ,c.Flavour ,"cake from map")
}else{
f.Cakes[flavour] = Cake{flavour}
println("put a new" ,flavour ,"cake into map")
}
return f.Cakes[flavour]
}
func main(){
factory := CakeFactory{make(map[string]Cake)}
factory.NewCake("strawberry")
factory.NewCake("chocolates")
factory.NewCake("nynicg")
factory.NewCake("strawberry")
factory.NewCake("nynicg")
factory.NewCake("chocolates")
/*
output:
put a new strawberry cake into map
put a new chocolates cake into map
put a new nynicg cake into map
get an existing strawberry cake from map
get an existing nynicg cake from map
get an existing chocolates cake from map
*/
}