Java 设计模式11——享元模式

享元模式

享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。

实现方式

  1. 采用HashMap或者其他支持KV访问的数据结构来存储这些创建的对象;
  2. 用唯一标识码判断,如果在内存中有,则返回这个唯一标识码所标识的对象;

代码示例

享元模式尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象。我们将通过创建 5 个对象来画出 20 个分布于不同位置的圆来演示这种模式。由于只有 5 种可用的颜色,所以 color 属性被用来检查现有的 Circle 对象。具体步骤如下:

  1. 定义Shape接口;
  2. 定义Circle对象;
  3. 定义产生Circle对象的工厂;
  4. 示例运行
1
2
3
public interface Shape {
void draw();
}
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
public class Circle implements Shape {
private String color;
private int x;
private int y;
private int radius;

public Circle(String color) {
this.color = color;
}

// 这里注释掉setColor方法,不然外界拿到对象后可以调用该方法来改变Circle颜色
// public void setColor(String color) {
// this.color = color;
// }

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public void setRadius(int radius) {
this.radius = radius;
}

@Override
public void draw() {
System.out.println("Circle: draw()[" + "color=" + color + ", x=" + x + ", y=" + y + ", radius=" + radius + ']');
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ShapeFactory {
private static final HashMap<String, Shape> circleMap = new HashMap<>();

public static Shape getCircle(String color) {
Circle circle = (Circle) circleMap.get(color);

if (circle == null) {
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("----------------------------------");
System.out.println("create circle of color:" + color);
System.out.println("----------------------------------");
}
return circle;
}
}
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
public class FlyWeightDemo {
private static final String[] colors = new String[]{"Red", "Green", "Blue", "Black", "Purple"};

public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.setRadius(100);
circle.draw();
}
}

private static String getRandomColor() {
return colors[(int) (Math.random() * colors.length)];
}

private static int getRandomX() {
return (int) (Math.random() * 100);
}

private static int getRandomY() {
return (int) (Math.random() * 100);
}
}

输出结果如下:

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
----------------------------------
create circle of color:Purple
----------------------------------
Circle: draw()[color=Purple, x=70, y=24, radius=100]
----------------------------------
create circle of color:Green
----------------------------------
Circle: draw()[color=Green, x=47, y=38, radius=100]
----------------------------------
create circle of color:Red
----------------------------------
Circle: draw()[color=Red, x=75, y=7, radius=100]
Circle: draw()[color=Red, x=52, y=73, radius=100]
----------------------------------
create circle of color:Black
----------------------------------
Circle: draw()[color=Black, x=74, y=6, radius=100]
----------------------------------
create circle of color:Blue
----------------------------------
Circle: draw()[color=Blue, x=30, y=31, radius=100]
Circle: draw()[color=Black, x=64, y=81, radius=100]
Circle: draw()[color=Green, x=17, y=98, radius=100]
Circle: draw()[color=Green, x=50, y=77, radius=100]
Circle: draw()[color=Blue, x=68, y=88, radius=100]
Circle: draw()[color=Red, x=56, y=90, radius=100]
Circle: draw()[color=Black, x=3, y=46, radius=100]
Circle: draw()[color=Black, x=48, y=51, radius=100]
Circle: draw()[color=Black, x=57, y=45, radius=100]
Circle: draw()[color=Green, x=71, y=20, radius=100]
Circle: draw()[color=Blue, x=94, y=85, radius=100]
Circle: draw()[color=Purple, x=33, y=91, radius=100]
Circle: draw()[color=Purple, x=55, y=46, radius=100]
Circle: draw()[color=Green, x=36, y=52, radius=100]
Circle: draw()[color=Black, x=48, y=72, radius=100]

模式总结

享元模式主要运用共享技术有效地支持大量细粒度的对象。在有大量对象时,有可能会造成内存溢出,我们把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重新创建。

优缺点

  • 优点:大大减少对象的创建,降低系统的内存,使效率提高。

  • 缺点:提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱。

使用场景

  1. 系统中有大量对象。
  2. 这些对象消耗大量内存。
  3. 这些对象的状态大部分可以外部化。
  4. 这些对象可以按照内蕴状态分为很多组,当把外蕴对象从对象中剔除出来时,每一组对象都可以用一个对象来代替
  5. 系统不依赖于这些对象身份,这些对象是不可分辨的。

应用实例

  1. JAVA 中的 String,如果有则返回,如果没有则创建一个字符串保存在字符串缓存池里面。
  2. 数据库的数据池。

注意事项

  1. 注意划分外部状态和内部状态,否则可能会引起线程安全问题。 示例中color对一个Circle来说是不变的,属于内部状态,而x、y、radius则属于外部状态,可以又外界直接改变。
  2. 这些类必须有一个工厂对象加以控制。