作者:湘西有堵墙 | 来源:互联网 | 2023-09-23 16:13
github:https://github.com/zhumengyifang/GolangDesignPatterns
上一篇外观设计模式:https://blog.csdn.net/weixin_40165163/article/details/90759617
享元设计模式
享元模式主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。
享元模式尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象。我们将通过创建5个对象来画出20个分布于不同位置的圆来演示这种模式,由于只有5种可用的颜色,所以color属性被用来检查现有的circle对象。
介绍:
意图:运用共享技术有效地支持大量细颗粒的对象。
主要解决:在有大量对象时,有可能会造成内存溢出,我们把其中共同的部分抽离出来,如果有相同的业务请求,直接返回在内存种已有的对象,避免重新创建。
何时使用:
- 系统中有大量对象
- 这些对象消耗大量内存
- 这些对象的状态大部分可以外部化
- 这些对象可以按照内蕴状态分为很多组,当把外蕴对象从对象中剔除出来时,每一组对象都可以用一个对象来代替
- 系统不依赖于这些对象身份,这些对象是不可分辨的
如何解决:用唯一标示判断,如果在内存中有,则返回这个唯一标示码的对象
关键代码:用Map存储这些对象
优点:大大减少对象的创建,降低系统的内存,使用效率高。
缺点:提高了系统的复杂度,需要分离出外部和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱。
使用场景:系统有大量相似对象
注意事项:
- 注意划分外部状态和内部状态,否则可能会引起线程安全问题
- 这些类必须有一个工厂对象加以控制
实现
我们将创建一个Shape接口和实现了Shape接口的实体类Circle。下一步是定义工厂类ShapeFactory。
ShapeFactory 有一个 Circle 的 Map,其中键名为 Circle 对象的颜色。无论何时接收到请求,都会创建一个特定颜色的圆。ShapeFactory检查它的 HashMap 中的 circle 对象,如果找到 Circle 对象,则返回该对象,否则将创建一个存储在 hashmap 中以备后续使用的新对象,并把该对象返回到客户端。
FlyWeightPatternDemo,我们的演示类使用 ShapeFactory 来获取 Shape 对象。它将向 ShapeFactory 传递信息(red / green / blue/ black / white),以便获取它所需对象的颜色。
![](https://img1.php1.cn/3cdc5/3345/807/e145511211ef2295.png)
代码:
package FlyweightPattern
import "fmt"
type Shape interface {
Draw3()
SetX(x int)
SetY(y int)
SetRadius(radius int)
}
type Circle struct {
color string
x int
y int
radius int
}
func (c *Circle) Circle(color string) {
c.color = color
}
func (c *Circle) Draw3() {
fmt.Println("Circle: Draw() [Color :", c.color, "x:", c.x, "y:", c.y, "radius:", c.radius)
}
func (c *Circle) SetX(x int) {
c.x = x
}
func (c *Circle) SetY(y int) {
c.y = y
}
func (c *Circle) SetRadius(radius int) {
c.radius = radius
}
package FlyweightPattern
import "fmt"
//创建一个工厂,生成基于给定消息的实体类的对象
type ShapeFactory struct {
circleMap map[string]Shape
}
func (s *ShapeFactory) GetCircle(color string) Shape {
if s.circleMap == nil {
s.circleMap = make(map[string]Shape)
}
circle := s.circleMap[color]
if circle == nil {
newCircle := new(Circle)
newCircle.Circle(color)
s.circleMap[color] = newCircle
fmt.Println("Creating circle of color : ", color)
circle = newCircle
}
return circle
}
测试:
package FlyweightPattern
import (
"math/rand"
)
type FlyweightPatternDemo struct {
colors []string
}
func (f *FlyweightPatternDemo) GetRandomColor() string {
if f.colors == nil {
f.colors = []string{"Red", "Green", "Blue", "White", "Black"}
}
num := rand.Intn(len(f.colors) - 1)
return f.colors[num]
}
func (f *FlyweightPatternDemo) GetRandomXAndY() int {
num := rand.Intn(10) * 100
return num
}
func testFlyweightPattern() {
flyweightPatternDemo := FlyweightPattern.FlyweightPatternDemo{}
shapeFactory := new(FlyweightPattern.ShapeFactory)
for i := 0; i <20; i++ {
circle := shapeFactory.GetCircle(flyweightPatternDemo.GetRandomColor())
circle.SetX(flyweightPatternDemo.GetRandomXAndY())
circle.SetY(flyweightPatternDemo.GetRandomXAndY())
circle.SetRadius(100)
circle.Draw3()
}
}
输出:
Creating circle of color : Green
Circle: Draw() [Color : Green x: 700 y: 700 radius: 100
Creating circle of color : White
Circle: Draw() [Color : White x: 100 y: 800 radius: 100
Circle: Draw() [Color : Green x: 0 y: 600 radius: 100
Creating circle of color : Red
Circle: Draw() [Color : Red x: 400 y: 100 radius: 100
Creating circle of color : Blue
Circle: Draw() [Color : Blue x: 900 y: 800 radius: 100
Circle: Draw() [Color : Blue x: 100 y: 500 radius: 100
Circle: Draw() [Color : Green x: 600 y: 500 radius: 100
Circle: Draw() [Color : Blue x: 800 y: 800 radius: 100
Circle: Draw() [Color : White x: 700 y: 700 radius: 100
Circle: Draw() [Color : Red x: 0 y: 500 radius: 100
Circle: Draw() [Color : Green x: 800 y: 700 radius: 100
Circle: Draw() [Color : White x: 900 y: 600 radius: 100
Circle: Draw() [Color : Green x: 100 y: 500 radius: 100
Circle: Draw() [Color : Blue x: 300 y: 0 radius: 100
Circle: Draw() [Color : Blue x: 300 y: 300 radius: 100
Circle: Draw() [Color : White x: 800 y: 400 radius: 100
Circle: Draw() [Color : White x: 300 y: 700 radius: 100
Circle: Draw() [Color : Green x: 900 y: 900 radius: 100
Circle: Draw() [Color : Red x: 500 y: 800 radius: 100
Circle: Draw() [Color : Blue x: 300 y: 500 radius: 100
总结:该种设计模式也是比较常用且好理解的设计模式,缓存,线程池等也是同等思路。
下一章:https://blog.csdn.net/weixin_40165163/article/details/90760597