mirror of
https://github.com/Estom/notes.git
synced 2026-02-10 05:46:20 +08:00
go知识重新整理
This commit is contained in:
65
Go/DesignPattern/flyWeight/flyWeight.go
Normal file
65
Go/DesignPattern/flyWeight/flyWeight.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/11 下午2:55
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package flyWeight
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 享元模式:从对象中剥离不发生改变,并且多个实例需要的重复数据,独立出来一个享元,让多个对象来共享,从而节省内存以及减少对象数量
|
||||
|
||||
type ImageFlyWeightFactory struct {
|
||||
maps map[string]*ImageFlyWeight
|
||||
}
|
||||
|
||||
var imageFatory *ImageFlyWeightFactory
|
||||
|
||||
func GetImageFlyFactory() *ImageFlyWeightFactory {
|
||||
if imageFatory == nil {
|
||||
imageFatory = &ImageFlyWeightFactory{
|
||||
maps: make(map[string]*ImageFlyWeight),
|
||||
}
|
||||
}
|
||||
return imageFatory
|
||||
}
|
||||
func (f *ImageFlyWeightFactory) Get(filename string) *ImageFlyWeight {
|
||||
image := f.maps[filename]
|
||||
if image == nil {
|
||||
image = NewImageFlyWeight(filename)
|
||||
f.maps[filename] = image
|
||||
}
|
||||
return image
|
||||
}
|
||||
|
||||
func NewImageFlyWeight(filename string) *ImageFlyWeight {
|
||||
//load image file
|
||||
data := fmt.Sprintf("image data %s", filename)
|
||||
return &ImageFlyWeight{
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
type ImageFlyWeight struct {
|
||||
data string
|
||||
}
|
||||
|
||||
func (i *ImageFlyWeight) Data() string {
|
||||
return i.data
|
||||
}
|
||||
|
||||
func NewImageViewr(filename string) *ImageViewer {
|
||||
image := GetImageFlyFactory().Get(filename)
|
||||
return &ImageViewer{
|
||||
ImageFlyWeight: image,
|
||||
}
|
||||
}
|
||||
|
||||
type ImageViewer struct {
|
||||
*ImageFlyWeight
|
||||
}
|
||||
|
||||
func (i *ImageViewer) Display() {
|
||||
fmt.Printf("Display:%s\n", i.Data())
|
||||
}
|
||||
17
Go/DesignPattern/flyWeight/flyweight_test.go
Normal file
17
Go/DesignPattern/flyWeight/flyweight_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/11 下午3:27
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package flyWeight
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFlyWeight(t *testing.T) {
|
||||
viewer1 := NewImageViewr("image1.png")
|
||||
viewer2 := NewImageViewr("image1.png")
|
||||
if viewer1.ImageFlyWeight != viewer2.ImageFlyWeight {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user