Singleton - Whether Cloneable overrides Singleton

Discussion in 'Java' started by Proton Projects - Moin, Mar 26, 2007.

  1. Hi all,

    public class ChummaEx implements Cloneable
    {
    private static ChummaEx moin = null;
    private String mName = null;
    private ChummaEx()
    {}

    public static ChummaEx getInstance()
    {
    if(moin == null)
    {
    createInstance();
    }
    return moin;
    }

    private static void createInstance()
    {
    if(moin == null)
    {
    moin = new ChummaEx();
    }
    }

    public void setName(String name)
    {
    mName = name;
    }

    public String getName()
    {
    return mName;
    }

    public static void main(String args[])
    {
    try
    {
    ChummaEx m1 = ChummaEx.getInstance();
    ChummaEx m2 = (ChummaEx)m1.clone();
    System.out.println(System.identityHashCode(m1));
    System.out.println(System.identityHashCode(m2));
    m1.setName("Moin");

    System.out.println(m2.getName());
    System.out.println(m1.getName());
    }
    catch(CloneNotSupportedException e)
    {
    System.out.println("In Catch");
    e.printStackTrace();
    }
    }
    }

    Output :

    11394033
    4384790
    null
    Moin

    SIngleton has the functionality to create only instance...I was trying
    to beak the singleton behavior and after a long research i achieved it
    by using the Cloneable interface...

    My Questions are:
    1. Whether the singleton behavior is overriden by the Cloneable
    behavior
    2. By implementing the Cloneable interface, how to retain the
    Singleton behavior...
    3. I tried to use the final key at some place to retain the singleton
    behavior....but i failed.
    4. Some group mates might have a question in their mind, why i have
    used the Cloneable interface....as i already told, i was doing some
    research over the singleton...

    Kindly help me in this regard
    Thanks
    Moin
     
    Proton Projects - Moin, Mar 26, 2007
    #1
    1. Advertising

  2. Proton Projects - Moin

    Daniel Pitts Guest

    On Mar 25, 11:43 pm, "Proton Projects - Moin"
    <> wrote:
    > Hi all,
    >
    > public class ChummaEx implements Cloneable
    > {
    > private static ChummaEx moin = null;
    > private String mName = null;
    > private ChummaEx()
    > {}
    >
    > public static ChummaEx getInstance()
    > {
    > if(moin == null)
    > {
    > createInstance();
    > }
    > return moin;
    > }
    >
    > private static void createInstance()
    > {
    > if(moin == null)
    > {
    > moin = new ChummaEx();
    > }
    > }
    >
    > public void setName(String name)
    > {
    > mName = name;
    > }
    >
    > public String getName()
    > {
    > return mName;
    > }
    >
    > public static void main(String args[])
    > {
    > try
    > {
    > ChummaEx m1 = ChummaEx.getInstance();
    > ChummaEx m2 = (ChummaEx)m1.clone();
    > System.out.println(System.identityHashCode(m1));
    > System.out.println(System.identityHashCode(m2));
    > m1.setName("Moin");
    >
    > System.out.println(m2.getName());
    > System.out.println(m1.getName());
    > }
    > catch(CloneNotSupportedException e)
    > {
    > System.out.println("In Catch");
    > e.printStackTrace();
    > }
    > }
    >
    > }
    >
    > Output :
    >
    > 11394033
    > 4384790
    > null
    > Moin
    >
    > SIngleton has the functionality to create only instance...I was trying
    > to beak the singleton behavior and after a long research i achieved it
    > by using the Cloneable interface...
    >
    > My Questions are:
    > 1. Whether the singleton behavior is overriden by the Cloneable
    > behavior
    > 2. By implementing the Cloneable interface, how to retain the
    > Singleton behavior...
    > 3. I tried to use the final key at some place to retain the singleton
    > behavior....but i failed.
    > 4. Some group mates might have a question in their mind, why i have
    > used the Cloneable interface....as i already told, i was doing some
    > research over the singleton...
    >
    > Kindly help me in this regard
    > Thanks
    > Moin


    You have to take steps toward making classes into singletons. One of
    those steps is to prevent the use of Cloneable. Also, if your
    singleton is serializable, you'll have to consider that as well.

    public final class MySingleton implements Serializable {
    public static final MySingleton instance = new MySingleton();

    private MySignleton() {
    }

    protected final Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
    }
    protected final Object readResolve() {
    return instance;
    }
    }
     
    Daniel Pitts, Mar 26, 2007
    #2
    1. Advertising

  3. Proton Projects - Moin

    Oliver Wong Guest

    "Proton Projects - Moin" <> wrote in message
    news:...
    > Hi all,
    >
    > public class ChummaEx implements Cloneable
    > {
    > private static ChummaEx moin = null;
    > private String mName = null;
    > private ChummaEx()
    > {}

    [Rest of the code snipped]
    > }
    >

    [...]
    >
    > SIngleton has the functionality to create only instance...I was trying
    > to beak the singleton behavior and after a long research i achieved it
    > by using the Cloneable interface...


    There are easier ways to "break" singleton behaviour. For example:

    public class ChummaEx {
    private static ChummaEx moin = null;
    private String mName = null;
    public ChummaEx()
    {}
    [Rest of the code snipped]
    }

    >
    > My Questions are:
    > 1. Whether the singleton behavior is overriden by the Cloneable
    > behavior
    > 2. By implementing the Cloneable interface, how to retain the
    > Singleton behavior...
    > 3. I tried to use the final key at some place to retain the singleton
    > behavior....but i failed.
    > 4. Some group mates might have a question in their mind, why i have
    > used the Cloneable interface....as i already told, i was doing some
    > research over the singleton...


    Decide whether you want your class to be a Singleton or not.

    If you do: Do not "break" the singleton behaviour. That is: do not
    provide a public constructor; do not implement cloneable; do not provide a
    factory method which returns lots of different instances, etc.

    If you don't: Do not worry if you "break" the singleton behaviour.

    - Oliver
     
    Oliver Wong, Mar 26, 2007
    #3
  4. Proton Projects - Moin

    Mark Rafn Guest

    Proton Projects - Moin <> wrote:
    [snip example of a Singleton class which is Cloneable]

    This makes no sense. A singleton is for sharing data across multiple uses,
    either because it's expensive to create the object or because there's
    logically only one in your app and you WANT all changes to be reflected in
    other uses of the object.

    Making it Cloneable breaks this pattern - now you have a non-singleton with
    one way of acquiring one that looks kind of singleton-like.

    >Singleton has the functionality to create only instance...I was trying
    >to beak the singleton behavior and after a long research i achieved it
    >by using the Cloneable interface...


    Cloning is tricky, and you should probably avoid it unless you have specific
    uses in mind. Generally, I'd recommend you go with a different pattern for
    this. Two options come to mind:
    1) Factory pattern. Change getInstance to newInstance(), and have it always
    new up an empty/default object. Have another factory method
    static ChummaEx copyOf(ChummaEx source)
    to give a new instance that copies relevant fields, and maybe other
    factory methods to give different versions.

    2) Normal object with public constructors, one of which is a copy
    constructor (takes a source ChummaEx to copy from).

    Note that BOTH of these ignore the singleton aspect of your example. But so
    does clone(). You're not using it as a singleton, so don't pretend it's
    a singleton.

    >1. Whether the singleton behavior is overriden by the Cloneable
    >behavior


    Clearly. You now have 2 objects of this type, how is that single?

    >2. By implementing the Cloneable interface, how to retain the
    >Singleton behavior...


    Don't. You don't want the Singleton behavior. Explain the situation better
    if you think you want both exactly-one and more-than-one instances
    simultaneously.

    >4. Some group mates might have a question in their mind, why i have
    >used the Cloneable interface....as i already told, i was doing some
    >research over the singleton...


    Your research has succeeded. You've discovered that it makes no sense to mix
    Singleton with patterns that create multiple instances.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Mar 26, 2007
    #4
  5. On Mar 27, 1:42 am, (Mark Rafn) wrote:
    > Proton Projects - Moin <> wrote:
    > [snip example of a Singleton class which is Cloneable]
    >
    > This makes no sense. A singleton is for sharing data across multiple uses,
    > either because it's expensive to create the object or because there's
    > logically only one in your app and you WANT all changes to be reflected in
    > other uses of the object.
    >
    > Making it Cloneable breaks this pattern - now you have a non-singleton with
    > one way of acquiring one that looks kind of singleton-like.
    >
    > >Singleton has the functionality to create only instance...I was trying
    > >to beak the singleton behavior and after a long research i achieved it
    > >by using the Cloneable interface...

    >
    > Cloning is tricky, and you should probably avoid it unless you have specific
    > uses in mind. Generally, I'd recommend you go with a different pattern for
    > this. Two options come to mind:
    > 1) Factory pattern. Change getInstance to newInstance(), and have it always
    > new up an empty/default object. Have another factory method
    > static ChummaEx copyOf(ChummaEx source)
    > to give a new instance that copies relevant fields, and maybe other
    > factory methods to give different versions.
    >
    > 2) Normal object with public constructors, one of which is a copy
    > constructor (takes a source ChummaEx to copy from).
    >
    > Note that BOTH of these ignore the singleton aspect of your example. But so
    > does clone(). You're not using it as a singleton, so don't pretend it's
    > a singleton.
    >
    > >1. Whether the singleton behavior is overriden by the Cloneable
    > >behavior

    >
    > Clearly. You now have 2 objects of this type, how is that single?
    >
    > >2. By implementing the Cloneable interface, how to retain the
    > >Singleton behavior...

    >
    > Don't. You don't want the Singleton behavior. Explain the situation better
    > if you think you want both exactly-one and more-than-one instances
    > simultaneously.
    >
    > >4. Some group mates might have a question in their mind, why i have
    > >used the Cloneable interface....as i already told, i was doing some
    > >research over the singleton...

    >
    > Your research has succeeded. You've discovered that it makes no sense to mix
    > Singleton with patterns that create multiple instances.
    > --
    > Mark Rafn <http://www.dagon.net/>


    Thanks all for your valuable solutions
    Moin
     
    Proton Projects - Moin, Mar 27, 2007
    #5
    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. cody
    Replies:
    13
    Views:
    830
    Bent C Dalager
    Aug 22, 2003
  2. Malcolm Ryan
    Replies:
    4
    Views:
    958
    Greg Stark
    Feb 1, 2005
  3. Guest

    Cloneable questions

    Guest, Feb 20, 2005, in forum: Java
    Replies:
    3
    Views:
    516
    Thomas Bühler
    Feb 21, 2005
  4. Thomas G. Marshall

    Why not cloneable by default?

    Thomas G. Marshall, Mar 19, 2005, in forum: Java
    Replies:
    142
    Views:
    3,436
    Chris Smith
    Apr 11, 2005
  5. lord trousers
    Replies:
    4
    Views:
    655
    James Kanze
    Jan 10, 2008
Loading...

Share This Page