• 设计模式之享元模式


    享元模式运用共享技术有效地支持大量细粒度的对象,主要用于减少创建对象的数量,以减少内存占用和提高性能。尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象。用唯一标识码判断,如果在内存中有,则返回这个唯一标识码所标识的对象。

    享元模式的优点是减少了对象的创建,降低了内存,提高效率;缺点是提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化。在系统中有大量相似对象或者需要缓冲池的场景可以使用享元模式。

    1. 创建形状接口及其实现;
    public interface Shape {
       void draw();
    }
    
    public class Circle implements Shape {
       private String color;
       private int x;
       private int y;
       private int radius;
     
       public Circle(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
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    1. 创建工厂生成基于给定信息的实体类对象;
    /**
     * 享元模式,减少对象的创建,利用唯一标识码,
     * 如果内存中已存在,则不进行创建
     */
    import java.util.HashMap;
    
    public class ShapeFactory {
        private static final Map<String, Shape> circleMap = new HashMap<>();
    
        public static Shape getShape(String color){
            Circle circle = (Circle) circleMap.get(color);
            if(circle == null){
                circle = new Circle(color);
                circleMap.put(color, circle);
                System.out.println("Creating circle of color:" + color);
            }
    
            return circle;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    1. 使用该工厂,传递不同颜色获取实体类对象。
    public class FlyweightInstance {
    
        private static final String[] colors = {"Red", "Green", "Blue"};
    
        public static void main(String[] args) {
            for(int i=0;i<20;i++){
                Circle circle = (Circle) ShapeFactory.getShape(getRandomColor());
                circle.setX(getRandom());
                circle.setY(getRandom());
                circle.setRadius(100);
                circle.draw();
            }
        }
    
        private static int getRandom(){
            return (int) (Math.random() * 100);
        }
    
        private static String getRandomColor(){
            return colors[(int)(Math.random()) * colors.length];
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    Matlab生成c代码,生成dll被Labview调用
    第一章python科学编程入门
    CSS篇十六——盒子模型之边框
    WorkPlus打造智慧企业移动门户,开启高效办公新时代
    leetcode 41. 缺失的第一个正数(困难)
    理解树状数组这一篇文章就够啦
    MR混合现实在临床医学课堂教学中的应用演示
    工业控制系统安全标准
    SOME/IP 协议介绍(二)
    idea 导入项目
  • 原文地址:https://blog.csdn.net/dolly_baby/article/details/126518539