-
Notifications
You must be signed in to change notification settings - Fork 246
/
flyweight.go
36 lines (30 loc) · 1.06 KB
/
flyweight.go
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
// Package flyweight is an example of the Flyweight Pattern.
package flyweight
import "fmt"
// Flyweighter interface
type Flyweighter interface {
Draw(width, height int, opacity float64) string
}
// FlyweightFactory implements a factory.
// If a suitable flyweighter is in pool, then returns it.
type FlyweightFactory struct {
pool map[string]Flyweighter
}
// GetFlyweight creates or returns a suitable Flyweighter by state.
func (f *FlyweightFactory) GetFlyweight(filename string) Flyweighter {
if f.pool == nil {
f.pool = make(map[string]Flyweighter)
}
if _, ok := f.pool[filename]; !ok {
f.pool[filename] = &ConcreteFlyweight{filename: filename}
}
return f.pool[filename]
}
// ConcreteFlyweight implements a Flyweighter interface.
type ConcreteFlyweight struct {
filename string // internal state
}
// Draw draws image. Args width, height and opacity is external state.
func (f *ConcreteFlyweight) Draw(width, height int, opacity float64) string {
return fmt.Sprintf("draw image: %s, width: %d, height: %d, opacity: %.2f", f.filename, width, height, opacity)
}