享元模式,主要就是一種池化方案,主要用于創(chuàng)建對象的數(shù)量,以減少內(nèi)存占用和提高性能。這種類型的設(shè)計模式屬于結(jié)構(gòu)型模式,它提供了減少對象數(shù)量從而改善應(yīng)用所需的對象結(jié)構(gòu)的方式。
享元模式,嘗試重用現(xiàn)有的同類對象,如果未找到相同匹配的對象,那么就去創(chuàng)建對象。享元模式的核心思想是共享相同狀態(tài)的對象,以節(jié)省系統(tǒng)資源和內(nèi)存空間。
在享元模式中,對象分為兩種類型:內(nèi)部狀態(tài)(Intrinsic State)和外部狀態(tài)(Extrinsic State)。
內(nèi)部狀態(tài)是對象可以共享的不變數(shù)據(jù),而外部狀態(tài)是隨著對象上下文的變化而變化的數(shù)據(jù)。通過將內(nèi)部狀態(tài)和外部狀態(tài)分離,享元模式使得多個對象可以共享相同的內(nèi)部狀態(tài),而外部狀態(tài)則可以在運行時設(shè)置。
使用享元模式的主要目的是在系統(tǒng)中有大量對象并且這些對象的大部分狀態(tài)是相同的情況下,減少對象的數(shù)量,節(jié)省內(nèi)存空間,并提高系統(tǒng)的性能。通過共享相同狀態(tài)的對象,可以避免重復(fù)創(chuàng)建相似的對象,從而減少內(nèi)存占用。這對于需要創(chuàng)建大量細(xì)粒度的對象的場景特別有用,例如富文本編輯器中的字母對象、游戲中的粒子系統(tǒng)等。
public interface Shape {
void draw();
}
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);
}
}
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;
}
}
public class FlyweightPatternDemo {
private static final String colors[] = { "Red", "Green", "Blue", "White", "Black" };
/**
我們將創(chuàng)建一個 Shape 接口和實現(xiàn)了 Shape 接口的實體類 Circle。下一步是定義工廠類 ShapeFactory。
ShapeFactory 有一個 Circle 的 HashMap,其中鍵名為 Circle 對象的顏色。無論何時接收到請求,都會創(chuàng)建一個特定顏色的圓。
ShapeFactory 檢查它的 HashMap 中的 circle 對象,如果找到 Circle 對象,則返回該對象,否則將創(chuàng)建一個存儲在 hashmap 中以備后續(xù)使用的新對象,并把該對象返回到客戶端。
FlyWeightPatternDemo 類使用 ShapeFactory 來獲取 Shape 對象。它將向 ShapeFactory 傳遞信息(red / green / blue/ black / white),以便獲取它所需對象的顏色
*/
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);
}
}
總結(jié)來說,享元模式的意圖是通過共享對象的方式有效地支持大量細(xì)粒度的對象,減少對象的創(chuàng)建和內(nèi)存占用。它可以提高系統(tǒng)的性能和資源利用率。使用該模式時,需要將對象的內(nèi)部狀態(tài)和外部狀態(tài)進(jìn)行合理劃分,確保內(nèi)部狀態(tài)是可以共享的,而外部狀態(tài)可以在運行時設(shè)置。文章來源:http://www.zghlxwxcb.cn/news/detail-531097.html
好處:
這樣可以實現(xiàn)對象的共享,避免重復(fù)創(chuàng)建相似的對象,從而達(dá)到節(jié)省內(nèi)存的目的。文章來源地址http://www.zghlxwxcb.cn/news/detail-531097.html
到了這里,關(guān)于二十三種設(shè)計模式第十三篇--享元模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!