zoukankan      html  css  js  c++  java
  • 结构型模式

    享元模式 flyweight pattern

    简介

    主要用于减少创建对象的数量,以减少内容占用和提高性能。
    尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象。

    意图

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

    特点

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

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

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

    1. Java中的String,如果有则返回,没有则创建一个,保存在字符串缓存池中;
    2. 数据库的数据池;
    3. 多线程中的线程池;
    • 优点
      大大减少对象的创建,降低系统的内存,使效率提高

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

    使用场景

    1. 系统有大量相似对象;
    2. 需要缓冲池的场景

    注意:

    1. 注意划分外部状态和内部状态,否则可能会引起线程安全问题;
    2. 这些类必须有一个工厂对象加以控制;

    UML类图

    实现代码

    1. 创建Shape接口
    // Shape.java
    public interface Shape {
          public void draw();
    }
    
    1. 创建Shape接口实体类Circle
    // Circle.java
    public class Circle implements Shape {
          private int x, y, radius;
          private String color;
          
          public Circle(String color) {
                this.color = color;
          }
          
          @Override
          public void draw() {
                System.out.println("Shape: Circle, position = ( " + x + ", "  + y + "), radius = " + radius + ", 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;
          }
    }
    
    1. 创建工厂ShapeFactory,基于给定信息生成实体类Circle,并持有一个HashMap管理实体类对象
    // ShapeFacotry.java
    public class ShapeFactory {
          private static final HashMap<String, Shape> circleMap = new  HashMap<>();
          
          public static Shape getCircle(String color) {
                Shape circle = (Circle)circleMap.get(color);
                if(null == circle) {
                      circle = new Circle(color);
                      circleMap.put(color, circle);
                      System.out.println("Creating circle of color: " +  color);
                }
                return circle;
          }
    }
    
    1. 使用该工厂,通过传递颜色信息来获取实体类对象
    // FlyWeightPatternDemo.java
    public class FlyWeightPatternDemo {
          private static final String colors[] = {"Red", "Green", "White",  "Black"};
          
          public static void main(String[] args) {
                ShapeFactory factory = new ShapeFactory();
                
                for(int i = 0; i < 20; i ++) {
                      Circle circle =  (Circle)factory.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);
          }
    }
    

    运行结果

    Creating circle of color: Black
    Shape: Circle, position = ( 51, 20), radius = 100, color = Black
    Creating circle of color: Red
    Shape: Circle, position = ( 91, 81), radius = 100, color = Red
    Creating circle of color: White
    Shape: Circle, position = ( 37, 31), radius = 100, color = White
    Shape: Circle, position = ( 52, 18), radius = 100, color = White
    Shape: Circle, position = ( 1, 78), radius = 100, color = Black
    Shape: Circle, position = ( 36, 54), radius = 100, color = Red
    Shape: Circle, position = ( 22, 30), radius = 100, color = White
    Shape: Circle, position = ( 65, 19), radius = 100, color = White
    Shape: Circle, position = ( 12, 74), radius = 100, color = Red
    Creating circle of color: Green
    Shape: Circle, position = ( 69, 91), radius = 100, color = Green
    Shape: Circle, position = ( 82, 3), radius = 100, color = Green
    Shape: Circle, position = ( 49, 64), radius = 100, color = Red
    Shape: Circle, position = ( 29, 66), radius = 100, color = Green
    Shape: Circle, position = ( 3, 33), radius = 100, color = Red
    Shape: Circle, position = ( 84, 10), radius = 100, color = White
    Shape: Circle, position = ( 46, 26), radius = 100, color = White
    Shape: Circle, position = ( 61, 8), radius = 100, color = Red
    Shape: Circle, position = ( 60, 35), radius = 100, color = White
    Shape: Circle, position = ( 13, 40), radius = 100, color = Green
    Shape: Circle, position = ( 85, 84), radius = 100, color = Red
    
  • 相关阅读:
    Spring Boot启动时执行初始化操作三种方法分享
    springboot自定义验证传值范围
    动态数据源玩起来
    多线程之Semaphore登录限流示例
    elementui表格自定义格式实现原理???
    31 Days of Windows Phone | Day #5 System Theming
    SQL 子查询关联查询和非关联查询 性能分享
    windows phone app 发布后在市场里找不到呢。
    APP Hub 应用发布失败,请问大家都是怎么设置可以成功提交哦
    WPF:Main方法到哪里去了?
  • 原文地址:https://www.cnblogs.com/fortunely/p/14267357.html
Copyright © 2011-2022 走看看