简体   繁体   English

此模式的名称是什么? (重复使用的举重)

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

For years, I referred to this as flyweight , but looking for a good description of flyweight, I noted that they all said the basic use case was to create a lot of lightweight objects, whereas my motivation is to avoid creating a lot of objects. 多年以来,我一直将其称为flyweight ,但是在寻找对flyweight的良好描述时,我注意到他们都说基本用例是创建许多轻量级对象,而我的动机是避免创建大量对象。

The pattern is about using one object, or a small number of objects, for sequentially referring to different parts of a larger data structure under the guise of a specific interface. 该模式是关于使用一个对象或少量对象,以特定接口为幌子依次引用较大数据结构的不同部分。 For instance, here is a class for an object that gives me a Number object that refers to parts (one part at a time) of a byte array for its actual data: 例如,这是一个对象的类,该类为我提供了一个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()); }
}

You could say this is an adapter, but it's a special kind of adapter in that it refers to part of a larger storage without duplicating the data, and also that it can be changed to refer to a different part, without creating a new object. 您可以说这是一个适配器,但是它是一种特殊的适配器,因为它引用较大存储的一部分而不复制数据,并且可以将其更改为引用不同的部分,而无需创建新对象。

Any takes on how I should refer to that pattern? 我应如何提及该模式?

轻盈的花纹感觉像装饰图案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM