java static factory method vs. constructor - object reuse

Discussion in 'Java' started by javaguy44, Sep 21, 2004.

  1. javaguy44

    javaguy44 Guest

    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
     
    javaguy44, Sep 21, 2004
    #1
    1. Advertising

  2. javaguy44 <> scribbled the following:
    > 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?


    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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To err is human. To really louse things up takes a computer."
    - Anon
     
    Joona I Palaste, Sep 21, 2004
    #2
    1. Advertising

  3. javaguy44

    VisionSet Guest

    "Joona I Palaste" <> wrote in message
    news:cipasv$q0o$...
    > javaguy44 <> scribbled the following:


    >
    > > class Foo
    > > {
    > > private Foo() {}

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


    >
    > 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.

    --
    Mike W
     
    VisionSet, Sep 21, 2004
    #3
  4. javaguy44

    javaguy44 Guest

    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



    Joona I Palaste <> wrote in message news:<cipasv$q0o$>...
    > javaguy44 <> scribbled the following:
    > > 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?

    >
    > 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.
     
    javaguy44, Sep 21, 2004
    #4
  5. javaguy44 <> scribbled the following:
    > 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.

    > Thanks for your reply


    > Joona I Palaste <> wrote in message news:<cipasv$q0o$>...
    >> javaguy44 <> scribbled the following:
    >> > 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?

    >>
    >> 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.


    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "How can we possibly use sex to get what we want? Sex IS what we want."
    - Dr. Frasier Crane
     
    Joona I Palaste, Sep 21, 2004
    #5
  6. javaguy44

    Eric Sosman Guest

    javaguy44 wrote:
    > 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.

    --
     
    Eric Sosman, Sep 21, 2004
    #6
  7. javaguy44

    javaguy44 Guest

    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.




    Eric Sosman <> wrote in message news:<>...
    > javaguy44 wrote:
    > > 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.
     
    javaguy44, Sep 22, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    5
    Views:
    562
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  2. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    441
    Hylander
    Feb 26, 2004
  3. Matthias Kaeppler
    Replies:
    22
    Views:
    1,004
    Thomas G. Marshall
    May 24, 2005
  4. Medi Montaseri
    Replies:
    17
    Views:
    921
    Medi Montaseri
    Sep 3, 2003
  5. code reuse and design reuse

    , Feb 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,066
    Malcolm
    Feb 12, 2006
Loading...

Share This Page