橘子味的心
标题:Java享元模式(Flyweight Pattern)

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

享元模式(Flyweight Pattern)尝试通过存储已经存在的类似对象以重用,并在找不到匹配的对象时创建新对象。我们将通过绘制不同位置的20个圆圈来演示这种模式,但是这里只创建5个对象。只有5种颜色可用,因此color属性用于检查已经存在的Circle对象。

实现实例

在这个实例中,将创建一个Shape接口和一个实现Shape接口的具体类Circle。在下一步中将定义一个工厂类ShapeFactory

ShapeFactory有一个HashMapCircle作为Circle对象的颜色。每当一个请求向ShapeFactory创建一个指定颜色的圆形时,它会检查HashMap中的圆形对象,如果找到对象则返回这个对象,否则就会创建一个新对象然后存储在hashmap中以供将来使用,并返回这个新创建的对象给客户端。

FlyWeightPatternDemo这是一个演示类,将使用ShapeFactory来获取一个Shape对象。它将信息(红色/绿色/蓝色/黑色/白色)传递给ShapeFactory以获得所需颜色的圆形。

享元模式的实现实例结构如下图中所示 -

第1步

创建一个接口,如下代码 -
Shape.java

  1. public interface Shape {
  2. void draw();
  3. }
  4. Java

第2步

创建一个实现相同接口的具体类。
Circle.java

  1. public class Circle implements Shape {
  2. private String color;
  3. private int x;
  4. private int y;
  5. private int radius;
  6.  
  7. public Circle(String color){
  8. this.color = color;
  9. }
  10.  
  11. public void setX(int x) {
  12. this.x = x;
  13. }
  14.  
  15. public void setY(int y) {
  16. this.y = y;
  17. }
  18.  
  19. public void setRadius(int radius) {
  20. this.radius = radius;
  21. }
  22.  
  23. @Override
  24. public void draw() {
  25. System.out.println("Circle: Draw() [Color : " + color + ", x : " + x + ", y :" + y + ", radius :" + radius);
  26. }
  27. }
  28. Java

第3步

创建一个工厂根据给定的信息生成具体类的对象。
ShapeFactory.java

  1. import java.util.HashMap;
  2.  
  3. public class ShapeFactory {
  4. private static final HashMap<String, Shape> circleMap = new HashMap();
  5.  
  6. public static Shape getCircle(String color) {
  7. Circle circle = (Circle)circleMap.get(color);
  8.  
  9. if(circle == null) {
  10. circle = new Circle(color);
  11. circleMap.put(color, circle);
  12. System.out.println("Creating circle of color : " + color);
  13. }
  14. return circle;
  15. }
  16. }
  17. Java

第4步

使用工厂并通过传递诸如颜色的信息来获得具体类的对象。

FlyweightPatternDemo.java

  1. public class FlyweightPatternDemo {
  2. private static final String colors[] = { "Red", "Green", "Blue", "White", "Black" };
  3. public static void main(String[] args) {
  4.  
  5. for(int i=0; i < 20; ++i) {
  6. Circle circle = (Circle)ShapeFactory.getCircle(getRandomColor());
  7. circle.setX(getRandomX());
  8. circle.setY(getRandomY());
  9. circle.setRadius(100);
  10. circle.draw();
  11. }
  12. }
  13. private static String getRandomColor() {
  14. return colors[(int)(Math.random()*colors.length)];
  15. }
  16. private static int getRandomX() {
  17. return (int)(Math.random()*100 );
  18. }
  19. private static int getRandomY() {
  20. return (int)(Math.random()*100);
  21. }
  22. }
  23. Java

第5步

验证输出,执行上面的代码得到以下结果 -

  1. Creating circle of color : Black
  2. Circle: Draw() [Color : Black, x : 36, y :71, radius :100
  3. Creating circle of color : Green
  4. Circle: Draw() [Color : Green, x : 27, y :27, radius :100
  5. Creating circle of color : White
  6. Circle: Draw() [Color : White, x : 64, y :10, radius :100
  7. Creating circle of color : Red
  8. Circle: Draw() [Color : Red, x : 15, y :44, radius :100
  9. Circle: Draw() [Color : Green, x : 19, y :10, radius :100
  10. Circle: Draw() [Color : Green, x : 94, y :32, radius :100
  11. Circle: Draw() [Color : White, x : 69, y :98, radius :100
  12. Creating circle of color : Blue
  13. Circle: Draw() [Color : Blue, x : 13, y :4, radius :100
  14. Circle: Draw() [Color : Green, x : 21, y :21, radius :100
  15. Circle: Draw() [Color : Blue, x : 55, y :86, radius :100
  16. Circle: Draw() [Color : White, x : 90, y :70, radius :100
  17. Circle: Draw() [Color : Green, x : 78, y :3, radius :100
  18. Circle: Draw() [Color : Green, x : 64, y :89, radius :100
  19. Circle: Draw() [Color : Blue, x : 3, y :91, radius :100
  20. Circle: Draw() [Color : Blue, x : 62, y :82, radius :100
  21. Circle: Draw() [Color : Green, x : 97, y :61, radius :100
  22. Circle: Draw() [Color : Green, x : 86, y :12, radius :100
  23. Circle: Draw() [Color : Green, x : 38, y :93, radius :100
  24. Circle: Draw() [Color : Red, x : 76, y :82, radius :100
  25. Circle: Draw() [Color : Blue, x : 95, y :82, radius :100
  26. Java