Is Java's String Intern a flyweight?

后端 未结 6 1456
醉酒成梦
醉酒成梦 2020-12-11 02:10

Does the implementation of Java\'s String memory pool follows flyweight pattern?

Why I have this doubt is, I see that there is no extrinsic state involved in Intern.

6条回答
  •  心在旅途
    2020-12-11 02:23

    You have correctly identified that both Interning and Flyweight are based on the same idea: caching and sharing common state.

    With a Flyweight, in the extreme case when there is no extrinsic state to store, only the pointer to the intrinsic state remains. Then there is no need for the extrinsic state to even be an object, the pointer itself can be the extrinsic state. That's when Flyweight has become Interning.

    Whether Interning "really" is or is not a kind of Flyweight is just a debate over definitions. What matters most is the understanding of how one can be viewed as a specialized instance of the other, so you're good.

提交回复
热议问题