享元模式(Flyweight Pattern)主要用于減少創(chuàng)建的對(duì)象數(shù)量,并減少內(nèi)存占用并提高性能。 這種類型的設(shè)計(jì)模式屬于結(jié)構(gòu)模式,因?yàn)樵撃J教峁┝藴p少對(duì)象計(jì)數(shù)的方法,從而改善應(yīng)用的對(duì)象結(jié)構(gòu)。
享元模式(Flyweight Pattern)嘗試通過(guò)存儲(chǔ)已經(jīng)存在的類似對(duì)象以重用,并在找不到匹配的對(duì)象時(shí)創(chuàng)建新對(duì)象。我們將通過(guò)繪制不同位置的20
個(gè)圓圈來(lái)演示這種模式,但是這里只創(chuàng)建5
個(gè)對(duì)象。只有5
種顏色可用,因此color
屬性用于檢查已經(jīng)存在的Circle
對(duì)象。
在這個(gè)實(shí)例中,將創(chuàng)建一個(gè)Shape
接口和一個(gè)實(shí)現(xiàn)Shape
接口的具體類Circle
。在下一步中將定義一個(gè)工廠類ShapeFactory
。
ShapeFactory
有一個(gè)HashMap
的Circle
作為Circle
對(duì)象的顏色。每當(dāng)一個(gè)請(qǐng)求向ShapeFactory
創(chuàng)建一個(gè)指定顏色的圓形時(shí),它會(huì)檢查HashMap
中的圓形對(duì)象,如果找到對(duì)象則返回這個(gè)對(duì)象,否則就會(huì)創(chuàng)建一個(gè)新對(duì)象然后存儲(chǔ)在hashmap
中以供將來(lái)使用,并返回這個(gè)新創(chuàng)建的對(duì)象給客戶端。
FlyWeightPatternDemo
這是一個(gè)演示類,將使用ShapeFactory
來(lái)獲取一個(gè)Shape
對(duì)象。它將信息(紅色/綠色/藍(lán)色/黑色/白色)傳遞給ShapeFactory
以獲得所需顏色的圓形。
享元模式的實(shí)現(xiàn)實(shí)例結(jié)構(gòu)如下圖中所示 -
創(chuàng)建一個(gè)接口,如下代碼 -
Shape.java
public interface Shape {
void draw();
}
創(chuàng)建一個(gè)實(shí)現(xiàn)相同接口的具體類。
Circle.java
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);
}
}
創(chuàng)建一個(gè)工廠根據(jù)給定的信息生成具體類的對(duì)象。
ShapeFactory.java
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("Creating circle of color : " + color);
}
return circle;
}
}
使用工廠并通過(guò)傳遞諸如顏色的信息來(lái)獲得具體類的對(duì)象。
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());
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);
}
}
驗(yàn)證輸出,執(zhí)行上面的代碼得到以下結(jié)果 -
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