簡體   English   中英

此模式的名稱是什么? (重復使用的舉重)

[英]What's the name of this pattern? (reused flyweight)

多年以來,我一直將其稱為flyweight ,但是在尋找對flyweight的良好描述時,我注意到他們都說基本用例是創建許多輕量級對象,而我的動機是避免創建大量對象。

該模式是關於使用一個對象或少量對象,以特定接口為幌子依次引用較大數據結構的不同部分。 例如,這是一個對象的類,該類為我提供了一個Number對象,該對象引用其字節數組中的實際數據(一次一個部分):

public final class LittleEndRef extends Number {
    private byte[] a;
    private int off;

    // This is the point: the fields are not finals, set in a constructor, which would require
    // creating a new object every time I want to address some postion of an array. I reuse the
    // same object to refer to different positions. (My motivation is to ensure that there is no
    // overhead from garbage collection, ever.)
    void setRef(byte[] a, int off) { this.a = a; this.off = off; }

    public byte byteValue() { return a[off]; }
    public short shortValue() { return (short)(a[off] | a[off+1]<<8); }
    public int intValue() { return a[off] | a[off+1]<<8 | a[off+2]<<16 | a[off+3]<<24; }
    public long longValue() { return a[off] | a[off+1]<<8 | a[off+2]<<16 | a[off+3]<<24 |
                              (long)(a[off+4] | a[off+5]<<8 | a[off+6]<<16 | a[off+7]<<24)<<32; }

    public float floatValue() { return Float.intBitsToFloat(intValue()); }
    public double doubleValue() { return Double.longBitsToDouble(longValue()); }
}

您可以說這是一個適配器,但是它是一種特殊的適配器,因為它引用較大存儲的一部分而不復制數據,並且可以將其更改為引用不同的部分,而無需創建新對象。

我應如何提及該模式?

輕盈的花紋感覺像裝飾圖案

暫無
暫無

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

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