軟件設(shè)計(jì)模式之享元模式
Flyweight模式主要用于減少創(chuàng)建的對(duì)象數(shù)量并減少內(nèi)存占用并提高性能。
Flyweight模式主要用于減少創(chuàng)建的對(duì)象數(shù)量并減少內(nèi)存占用并提高性能。這種類型的設(shè)計(jì)模式屬于結(jié)構(gòu)模式,因?yàn)樵撃J教峁┝藴p少對(duì)象數(shù)量的方式,從而改善了應(yīng)用程序的對(duì)象結(jié)構(gòu)。
Flyweight模式嘗試通過(guò)存儲(chǔ)已存在的同類對(duì)象來(lái)重用它們,并在找不到匹配的對(duì)象時(shí)創(chuàng)建新對(duì)象。我們將通過(guò)繪制20個(gè)不同位置的圓圈來(lái)演示這種模式,但我們只會(huì)創(chuàng)建5個(gè)對(duì)象。只有5種顏色可用,因此color屬性用于檢查現(xiàn)有的Circle對(duì)象。
實(shí)作
我們將創(chuàng)建一個(gè)Shape接口和實(shí)現(xiàn)Shape接口的具體類Circle。下一步將定義工廠類ShapeFactory。
ShapeFactory有一個(gè)Circle的HashMap,其鍵為Circle對(duì)象的顏色。每當(dāng)請(qǐng)求向ShapeFactory創(chuàng)建特定顏色的圓時(shí),它都會(huì)在其HashMap中檢查圓形對(duì)象,如果找到了Circle對(duì)象,則將返回該對(duì)象,否則將創(chuàng)建一個(gè)新對(duì)象,將其存儲(chǔ)在hashmap中以備將來(lái)使用,并返回給客戶。
FlyWeightPatternDemo,我們的演示課,將使用ShapeFactory得到一個(gè)形狀對(duì)象。它將信息(紅色/綠色/藍(lán)色/黑色/白色)傳遞給ShapeFactory以獲得所需顏色的圓。

第1步
創(chuàng)建一個(gè)接口。
Shape.java
public interface Shape {
void draw();
}
第2步
創(chuàng)建實(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);
}
}
第3步
創(chuàng)建一個(gè)工廠,根據(jù)給定的信息生成具體類的對(duì)象。
ShapeFactory.java
import java.util.HashMap;
public class ShapeFactory {
// Uncomment the compiler directive line and
// javac *.java will compile properly.
// @SuppressWarnings("unchecked")
private static final HashMap 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;
}
}
第4步
使用工廠通過(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);
}
}
第5步
驗(yàn)證輸出。
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
