No, sharing objects to reduce memory is insufficient to call it a flyweight. In other words, caching is not automatically the flyweight pattern.
I think it would be fair to say that flyweight is a special form of caching, i.e. partial caching; but do note the GoF book does not use the words "cache" or "caching" anywhere in the flyweight chapter (though the terms are used in both the previous and subsequent chapters, facade and proxy, respectively).
A couple of comments in this thread are worth repeating, because they succinctly answer the overall question.
If there is no extrinsic context for your objects, then you are just caching. The whole reason the Flyweight pattern is even useful
to define, is that people often forget they can at least cache a part
of the object that is independent of context and share it.
--C S
Flyweight is about sharing the object internals. Interning is just caching the whole objects.
--Marko Topolnik
But let's compare String interning to the criteria that the GoF have defined (on page 197).
Apply the Flyweight pattern when all of the following are true:
- An application uses a large number of objects.
- Storage costs are high because of the sheer quantity of objects.
- Most object state can be made extrinsic.
- Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed.
- The application doesn't depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects.
- Clearly, many applications use a large number of Strings, so this criterion passes.
- Storing Strings is expensive, at least compared to primitive types, so let's give this criterion a pass.
- Here's where we get tripped up: none of a String's state is made extrinsic. This criterion fails.
- If we're generous and ignore the part about extrinsic state, we could give this criterion a pass as well, since Strings do tend to be reused.
- Anyone who's ever used
==
to compare Strings in Java knows not to depend on object identity, so this criterion passes.
Well 4/5 passing criteria is pretty good right? Shouldn't that be enough to say that interning/caching and flyweight are the same? No: similar != same. The emphasis on the word all in the GoF quote is theirs, not mine. There is naturally a strong desire to label as many implementations as possible with GoF pattern names, because doing so lends legitimacy to those implementations. (The most egregious cases are the factory patterns, which you can easily find labeling every kind of creational code imaginable; but I digress.) If the patterns are not held to their published definitions, they overlap and lose meaning, defeating a large part of their purpose (common vocabulary).
Lastly, let's analyze the first sentence of the flyweight chapter: what the GoF defines as the Intent of the flyweight pattern.
Use sharing to support large numbers of fine-grained objects efficiently.
I submit that an object with no extrinsic state is not fine-grained, but rather the opposite; so here is a suggested Intent for caching: Use caching to support large numbers of coarse-grained objects efficiently.
Clearly there is similarity between String interning/caching and the Flyweight Pattern; but they are not the same.