设计模式十九享元模式:边学边做个生成电子书的工具(python+dart)

106 阅读3分钟

意图:运用共享技术有效地支持大量细粒度的对象。

主要解决:在有大量对象时,有可能会造成内存溢出,我们把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重新创建。

何时使用:

1、系统中有大量对象。

2、这些对象消耗大量内存。

3、这些对象的状态大部分可以外部化。

4、这些对象可以按照内蕴状态分为很多组,当把外蕴对象从对象中剔除出来时,每一组对象都可以用一个对象来代替。

5、系统不依赖于这些对象身份,这些对象是不可分辨的。

如何解决:用唯一标识码判断,如果在内存中有,则返回这个唯一标识码所标识的对象。

image.png

python实现

class FlyWeight:
    def __init__(self,s):
        self.s = s

    def say(self):
        print(f"{self.s}:我是池子的一员")

class FlyWeightFactory:
    def __init__(self):
        self.d={}

    def getFlyWeight(self,s):
        if s in self.d:
            return  self.d[s]
        else:
            r = FlyWeight(s)
            self.d[s] = r
            return r


if __name__ =="__main__":
    f = FlyWeightFactory()
    d1 = f.getFlyWeight("one")
    d1.say()
    d2 = f.getFlyWeight("two")
    d2.say()
    d3 = f.getFlyWeight("one")
    d3.say()

    // E:\source\python\code2pdfword\venv\Scripts\python.exe
    //E: / source / python / code2pdfword / designpattern / dp / flyweight.py
    //one: 我是池子的一员
    //two: 我是池子的一员
    //one: 我是池子的一员

dart 实现

import 'dart:math';

abstract class Shape {
  void draw();
}

class Circle implements Shape {
  late String color;
  late int x;
  late int y;
  late int radius;
  Circle(this.color);
  @override
  void draw() {
    print("Circle: Draw() [Color : $color, x : $x, y :$y, radius :$radius");
  }
}

class ShapeFactory {
  static final Map<String, Shape> circleMap = Map();

  static Shape getCircle(String color) {
    Circle? circle = circleMap[color] as Circle?;
    if (circle == null) {
      circle = Circle(color);
      circleMap[color] = circle;
      print("Creating circle of color :$color");
    }
    return circle;
  }
}

void main(List<String> args) {
  final List<String> colors = ["red", "green", "blue", "white", "black"];
  for (int i = 0; i < 20; i++) {
    Shape circle =
        ShapeFactory.getCircle(colors[Random().nextInt(colors.length)]);
    (circle as Circle).x = Random().nextInt(100);
    (circle).y = Random().nextInt(100);
    (circle).radius = 100;
    circle.draw();
  }
}
// [Running] dart "e:\source\dart\designpatterns\flyweight.dart"
// Creating circle of color :black
// Circle: Draw() [Color : black, x : 77, y :60, radius :100
// Creating circle of color :green
// Circle: Draw() [Color : green, x : 41, y :70, radius :100
// Circle: Draw() [Color : black, x : 47, y :5, radius :100
// Creating circle of color :blue
// Circle: Draw() [Color : blue, x : 23, y :33, radius :100
// Circle: Draw() [Color : black, x : 0, y :51, radius :100
// Circle: Draw() [Color : blue, x : 46, y :17, radius :100
// Circle: Draw() [Color : blue, x : 69, y :72, radius :100
// Circle: Draw() [Color : blue, x : 39, y :14, radius :100
// Circle: Draw() [Color : blue, x : 49, y :51, radius :100
// Creating circle of color :red
// Circle: Draw() [Color : red, x : 41, y :51, radius :100
// Circle: Draw() [Color : blue, x : 93, y :89, radius :100
// Circle: Draw() [Color : black, x : 96, y :10, radius :100
// Circle: Draw() [Color : green, x : 23, y :19, radius :100
// Circle: Draw() [Color : blue, x : 55, y :65, radius :100
// Creating circle of color :white
// Circle: Draw() [Color : white, x : 16, y :89, radius :100
// Circle: Draw() [Color : black, x : 13, y :18, radius :100
// Circle: Draw() [Color : black, x : 69, y :72, radius :100
// Circle: Draw() [Color : red, x : 47, y :31, radius :100
// Circle: Draw() [Color : blue, x : 73, y :13, radius :100
// Circle: Draw() [Color : green, x : 80, y :99, radius :100

// [Done] exited with code=0 in 1.987 seconds