Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need a Class that supports interning, like Java's String class. When creating an instance of the Class it should return a pre-existing reference if a reference with the same parameters already exists. Otherwise it should return a new reference. Such that:

Foo a = new Foo(5);
Foo b = new Foo(5);
Foo c = new Foo(6);
a == b
a != c

1) Is it the job of a factory to maintain the table of pre-existing objects, or is the table generally just a static variable within the class itself?

2) What is the table of pre-existing objects? Is it a Set of objects you search through every time an instance is requested, or is it a Map where keys are objects and values are parameters?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
533 views
Welcome To Ask or Share your Answers For Others

1 Answer

Two things come to my mind

  1. Flyweight Pattern

  2. Guava's ComputingMap


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...