设计模式学习笔记--享元模式

十二、享元模式

享元模式(Flyweight Pattern)以较少对象的创建,来较少内存占用提高效率。它通过尝试重用现有的同类对象来实现共享已有对象。

简介

享元模式主要用于有大量对象时,避免造成内存溢出,然后将类对象共有部分抽象出来,较少对象的创建。
每个对象在内存中有唯一标识码,使用HashMap来存储对象。
- 优点:减少对象创建,提高系统内存空间和效率。
- 缺点:提高了系统的复杂度,需要分理处内部状态和外部状态。

实现

创建一个Shape接口和它的实现类Circle。定义一个工厂类Shape Factory。
在ShapeFactory中有一个Circle的HashMap,不同的Circle对象通过键值对类存储到HashMap中。
在使用时候,来调用,这有点类似Android中对ImageDrawable的加载缓存技术。
flyweight
- 创建接口

Shape.java

public interface Shape {
  void draw();
}
  • 接口实现类

Circle.java

public class Circle implements Shape {
  private String color;//颜色
  private int x;
  private int y;
  private int radius;//圆的坐标x,y和半径r

  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);

  }
}
  • 创建工厂类

ShapeFactory.java用于生成不同颜色的circle对象。

import java.util.HashMap;

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("Create circle of color: "+ color);
    } 
    return circle;
  }
}
  • 演示

FlyweightPatternDemo.java

public class FlyweightPatternDemo {
  private static final String colors[] = {"Red","Green","Blue","White","Black"};

  public static void main(String[] args){
    for(int i=0;i<20;++i){
      Circle circle = (Circle)ShapeFactory.getCircle(getRandomColor());
      cirlce.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
Circle: Draw() [Color : Black, x : 36, y :71, radius :100
Creating circle of color : Green
Circle: Draw() [Color : Green, x : 27, y :27, radius :100
Creating circle of color : White
Circle: Draw() [Color : White, x : 64, y :10, radius :100
Creating circle of color : Red
Circle: Draw() [Color : Red, x : 15, y :44, radius :100
Circle: Draw() [Color : Green, x : 19, y :10, radius :100
Circle: Draw() [Color : Green, x : 94, y :32, radius :100
Circle: Draw() [Color : White, x : 69, y :98, radius :100
Creating circle of color : Blue
Circle: Draw() [Color : Blue, x : 13, y :4, radius :100
Circle: Draw() [Color : Green, x : 21, y :21, radius :100
Circle: Draw() [Color : Blue, x : 55, y :86, radius :100
Circle: Draw() [Color : White, x : 90, y :70, radius :100
Circle: Draw() [Color : Green, x : 78, y :3, radius :100
Circle: Draw() [Color : Green, x : 64, y :89, radius :100
Circle: Draw() [Color : Blue, x : 3, y :91, radius :100
Circle: Draw() [Color : Blue, x : 62, y :82, radius :100
Circle: Draw() [Color : Green, x : 97, y :61, radius :100
Circle: Draw() [Color : Green, x : 86, y :12, radius :100
Circle: Draw() [Color : Green, x : 38, y :93, radius :100
Circle: Draw() [Color : Red, x : 76, y :82, radius :100
Circle: Draw() [Color : Blue, x : 95, y :82, radius :100
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值