java static factory method vs. constructor - object reuse

J

javaguy44

Hi,

I'm just picked up Bloch's Effective Java and had a question about the
reuse of objects when using a static factory method.

Take the following:

class Foo
{
private Foo() {}

public static Foo getInstance() {
return new Foo()
}
}

How is an object of Foo being reused? All I see is that every time
static getInstance is called, it returns a new Foo object. What am I
not seeing? Am I missing something underneath the hood that the JVM
is checking?

One more question - can only immutable class objects be reused?

Thanks,
Javaguy
 
J

Joona I Palaste

javaguy44 said:
I'm just picked up Bloch's Effective Java and had a question about the
reuse of objects when using a static factory method.
Take the following:
class Foo
{
private Foo() {}
public static Foo getInstance() {
return new Foo()
}
}
How is an object of Foo being reused? All I see is that every time
static getInstance is called, it returns a new Foo object. What am I
not seeing? Am I missing something underneath the hood that the JVM
is checking?

An object of Foo is not being reused. The code above is obviously only
an example of what might be done. As it stands, it offers no benefit
whatsoever over a public constructor. But it could be expanded into
either a Singleton or a Factory.
For a Singleton, getInstance() should reuse the same Foo object. For a
Factory, it should do something with the Foo it creates, or have the
opportunity to return a subclass of Foo.
One more question - can only immutable class objects be reused?

No, there is no such restriction. Reuse any class you want to.
 
V

VisionSet

Joona I Palaste said:
javaguy44 <[email protected]> scribbled the following:
An object of Foo is not being reused. The code above is obviously only
an example of what might be done. As it stands, it offers no benefit
whatsoever over a public constructor. But it could be expanded into
either a Singleton or a Factory.

I think it is worth pointing out that as it stands it actually offers a huge
benefit over a public constructor, namely that it has the potential to
supply an object of Foo OR ANY SUBCLASS. It is therefore much more flexible
and means that modification to the initial instance can be done in one place
from a maintenance point of view.
When you extend this pattern to return an interface type you gain even more
flexibility. Obviously this is not a reason to plaster the pattern
everywhere, but when these are potential requirements it should be
considered, it is therefore a widely used pattern, after Adapter, perhaps
the most common.
 
J

javaguy44

Hi Joona,

Have you read Effective Java? I think you are just reconfirming my
question, because unless you misunderstood me, as far as I see, an
object of Foo is not being reused. But unless I misinterpreted, Mr.
Bloch says that object's do cache when using static factory methods.
I just don't know how.

I know about Singleton and returning a different subclass within the
static factory...I'm mainly wondering about object reuse and caching

Thanks for your reply
 
J

Joona I Palaste

javaguy44 said:
Hi Joona,
Have you read Effective Java? I think you are just reconfirming my
question, because unless you misunderstood me, as far as I see, an
object of Foo is not being reused. But unless I misinterpreted, Mr.
Bloch says that object's do cache when using static factory methods.
I just don't know how.

Like I said, in the exact code you posted, no Foo object is ever
getting reused. Using a static factory method does not, by itself,
make objects be reused.
I know about Singleton and returning a different subclass within the
static factory...I'm mainly wondering about object reuse and caching

If you already know about Singleton, then you should already know how
to reuse an object in the static factory method.
 
E

Eric Sosman

javaguy44 said:
Hi Joona,

Have you read Effective Java? I think you are just reconfirming my
question, because unless you misunderstood me, as far as I see, an
object of Foo is not being reused. But unless I misinterpreted, Mr.
Bloch says that object's do cache when using static factory methods.
I just don't know how.

You may have misunderstood what Bloch wrote. What he
probably wrote (my copy is at home where I can't check it
right now) is that factory methods *can* be used as part
of an object-reuse scheme. The code you showed does not
provide for reuse, but here's one way it could be done:

class Thing {

/* Don't let outsiders use the constructor */
private Thing() { }

/* "Reservoir" is some sort of a container in
* which created but currently unused objects
* live.
*/
private static Reservoir stash = new Reservoir();

/* Give the caller a Thing, either newly-minted
* or retrieved from the stash.
*/
public static Thing thingFactory() {
Thing it = stash.removeNextThing();
if (it == null) // stash was empty
it = new Thing();
return it;
}

/* When the caller no longer needs this Thing,
* releaseThing() puts it into the stash where
* it becomes available for reuse.
*/
public static void releaseThing(Thing it) {
stash.insert(it);
}
}

That's a very simple outline. In practice, the factory
method might initialize each recycled Thing to a known state
(just as a constructor would), and might even take constructor-
like parameters. There might be provisions to keep the stash
from getting obscenely large; there might even be provisions
to let the Things in the stash be garbage-collected at need.
The fundamental idea, though, is that an object that is no
longer needed isn't just dropped on the floor for GC to sweep
up, but is instead held in a "recycle bin" for future reuse.
When another instance is needed, the factory method can provide
one by dusting off an already-used object instead of creating
a brand-new one.
 
J

javaguy44

Yup...I misunderstood what Block wrote....which is why I posted in the
first place...it totally didn't make sense to me as I first read it.

Thanks for your replies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top