享元模式(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