享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。
享元模式尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象
关键代码:用 HashMap 存储这些对象。
优点:大大减少对象的创建,降低系统的内存,使效率提高。
缺点:提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱。
注意事项: 1、注意划分外部状态和内部状态,否则可能会引起线程安全问题。 2、这些类必须有一个工厂对象加以控制。
- 创建一个接口。
/**
* 1. 创建一个接口
* @author mazaiting
*/
public interface Shape {
/**
* 画图
*/
void draw();
}
- 创建实现接口的实体类。
/**
* 2. 创建实现接口的实体类。
*
* @author mazaiting
*/
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;
}
public void draw() {
System.out.println("Circle: Draw() [Color : " + color + ", x : " + x
+ ", y :" + y + ", radius :" + radius);
}
}
- 创建一个工厂,生成基于给定信息的实体类的对象。
/**
* 3. 创建一个工厂,生成基于给定信息的实体类的对象。
* @author mazaiting
*/
public class ShapeFactory {
private static final HashMap<String, Shape> circleMap = new HashMap<String, Shape>();
public static Shape getCircle(String color) {
Circle 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;
}
}
- 使用该工厂,通过传递颜色信息来获取实体类的对象。
/**
* 4. 使用该工厂,通过传递颜色信息来获取实体类的对象。
* @author mazaiting
*/
public class Client {
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());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.setRadius(100);
circle.draw();
}
}
/**
* 获取随机颜色
*/
private static String getRandomColor() {
return colors[(int) (Math.random()*colors.length)];
}
/**
* 获取随机x值
*/
private static int getRandomX() {
return (int)(Math.random()*100 );
}
/**
* 获取随机y值
*/
private static int getRandomY() {
return (int)(Math.random()*100);
}
}
- 打印结果
Creating circle of color : Green
Circle: Draw() [Color : Green, x : 36, y :79, radius :100
Creating circle of color : Black
Circle: Draw() [Color : Black, x : 31, y :96, radius :100
Circle: Draw() [Color : Black, x : 78, y :15, radius :100
Circle: Draw() [Color : Black, x : 7, y :5, radius :100
Creating circle of color : Red
Circle: Draw() [Color : Red, x : 54, y :12, radius :100
Circle: Draw() [Color : Red, x : 3, y :30, radius :100
Creating circle of color : Blue
Circle: Draw() [Color : Blue, x : 3, y :21, radius :100
Circle: Draw() [Color : Red, x : 54, y :33, radius :100
Creating circle of color : White
Circle: Draw() [Color : White, x : 47, y :3, radius :100
Circle: Draw() [Color : Green, x : 27, y :50, radius :100
Circle: Draw() [Color : Blue, x : 9, y :27, radius :100
Circle: Draw() [Color : Black, x : 60, y :69, radius :100
Circle: Draw() [Color : Black, x : 95, y :2, radius :100
Circle: Draw() [Color : Black, x : 93, y :86, radius :100
Circle: Draw() [Color : Black, x : 62, y :82, radius :100
Circle: Draw() [Color : Blue, x : 15, y :48, radius :100
Circle: Draw() [Color : Red, x : 57, y :71, radius :100
Circle: Draw() [Color : Black, x : 94, y :63, radius :100
Circle: Draw() [Color : Red, x : 68, y :22, radius :100
Circle: Draw() [Color : White, x : 95, y :56, radius :100