簡體   English   中英

策略模式重用數據以創建報告

[英]Strategy Pattern reusing data to create a report

我正在實現一個報告,該報告將使用不同的組件,即某些組件具有頁眉,頁腳表。 另一個具有標題,標題,表格,圖形。 我已經使用與策略模式類似的模式來實現了這一點。 我可以使用相同的類報告生成報告,並具有定義了Component(onDraw)的接口。 每個組件都實現表格,圖形等...

但是,由於內存消耗和良好的軟件設計,如果在具有相同數據的每個報表上使用重復的表和標頭,我就不必創建重復的表和標頭。 有沒有一種模式可以用來將繪制的表格和標題從一個報表中保存下來,然后再用於另一個報表? 我一直在看蒼蠅的體重模式。 或在類報告上使用靜態變量。 問題是當我想在報表類上使用其他數據時。

我假設通過問這個問題,存在運行時未知數,這些未知數使您無法提前確定報表中哪些項目相同。 否則,您可以直接直接引用相同的實例。

緩存“等效”實例的flyweight風格的工廠可以幫助減少內存占用。 每個ReportComponent都需要某種參數對象來封裝其特定的數據字段並實現equals()來定義“等效”的含義。

public class ReportComponentFactory {

    private final Map<String, ReportComponent> headerCache = 
        new HashMap<String, ReportComponent>();
    private final Map<GraphParameters, ReportComponent> graphCache = 
        new HashMap<GraphParameters, ReportComponent>();

    public ReportComponent buildHeader(String headerText){
        if (this.headerCache.containsKey(headerText)){
            return this.headerCache.get(headerText);
        }
        Header newHeader = new Header(headerText);
        this.headerCache.put(headerText, newHeader);
        return newHeader;
    }

    public ReportComponent buildGraph(GraphParameters parameters){
        if (this.graphCache.containsKey(parameters)){
            return this.graphCache.get(parameters);
        }
        Graph newGraph = new Graph(parameters);
        this.graphCache.put(newGraph);
        return newGraph;
    }

    ...
}

請注意,實例化參數對象將需要一些臨時內存消耗,但是應該足夠容易地對其進行垃圾回收。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM